Well, almost, there is   a very subtle difference. If you define a function of either of the above; then, you can’t do the other. Or you will see an

Meet some mind-blowing ways to define functions in Julia: a tour of the many ways to create functions in Julia

submited by
Style Pass
2021-07-22 23:00:08

Well, almost, there is a very subtle difference. If you define a function of either of the above; then, you can’t do the other. Or you will see an error like this

Macros run the expression it returns. Since the above is return a :() block. Everything in between :() is the code one wishes to run when one runs the macro @defining_function and the $fnname interpolates the fnname symbol, which takes the form of name4 in the above example. Hence a new function called name4 is defined by @defining_function name4 .

Well fn(2,3) returns 6 . Can you see it now? c always take the value 1 in the a+b+c , because fn was generated by fn_generator(1) which assigns c=1 in its closure.

What would happen if you run the above? Will there be a new function called name being available for use? No. As the name name is captured by the fn_generator2 ‘s closure and is not available outside of that.

Being a pure mathematician (wanna be), I think of functors as something else, but apparently, the C++ crowd has been calling the below functors. Please educate me in the comment if I am wrong.

Leave a Comment