I am still exploring Elixir anonymous function, let’s move on to another interesting feature. Like javascript or functional programming language, in Elixir we can writes function that takes one or more functions as an input and or returns a function,
We have passed_func which is a function that returns a function, and apply_func which is takes function as an argument. Inside apply_func we apply passed_func twice.
Like Ruby, inside Elixir Enum module has a function called map which has the same purpose, different with Ruby, in Elixir the map function takes two arguments, first is a collection and second is a function.
ruby way:
elixir way
The & Shortcut
Again like a Ruby, Elixir has & operator to write short function. The body of the function surrounded by ( and ), and the the placeholders &1, &2, and so on correspond to the first, second, and subsequent parameters of the function.
This shortcut function can be applied in map function:
ruby way
elixir way