Anonymous functions
Anonymous functions are shorthand notations for regular functions. These are the choice of code when a function has to be used only a limited number of times, hence, it may be slightly easier and quicker to have them rather than using named functions. In popular terms, they are also sometimes referred to as lambda functions.
To relate to the preceding sentence, just think of a scenario wherein you want to apply a functionality over a list of values using a map()
function. Instead of writing down a full-fledged function, we can just define them in an easy way without even bothering about giving them a name!
In Julia, we define an anonymous function using the following syntax:
f -> 2f
The syntax uses ->
to notify that we are defining an anonymous function here. However, it should be kept in mind that anonymous functions themselves have no use, as they don't have a name, hence cannot be called from anywhere in the code:
julia> f ->2f (::#1) (generic function with...