August 01, 2017
Anonymous functions are simply functions or methods that is not assigned a name. While named functions are very much a common thing, anonymous functions has its good uses too especially in Elixir.
hello_world = fn -> IO.puts("Hello world") end
hello_world.() # Hello world
multiply = fn(a, b) -> a * b end
multiply.(5, 5) # 25
answer = fn
"Yes" -> "You are correct"
"No" -> "You are wrong"
_ -> "I cannot understand your answer"
end
answer.("Yes") # You are correct
answer.("No") # You are wrong
answer.("Maybe") # I cannot understand your answer
outer = fn ->
fn(x) -> "Inner function with value: #{x}" end
end
outer.().(30) # Inner function with value: 30
fact = "I love Elixir"
fun = fn -> "Hey, #{fact}" end
fun.() # Hey, I love Elixir
fun = fn(name) -> "Hello #{name}" end
# ... inside a module ...
def accept(fun, value) do
IO.puts fun.(value)
end
Module.accept(fun, "Elixir Programmer!") # Hello Elixir Programmer!
The above example basically shows how some of Enum
’s method work.
# Enum.map
list = [1, 2, 3, 4]
add_one = fn(number) -> number + 1 end
Enum.map list, add_one # [2, 3, 4, 5]
&
as a shortcut# using the longhand version
elixir = fn -> IO.puts("longhand version") end
# using the ampersand version
elixir = &(IO.puts(&1))
&1
is basically a call to the first argument. You can use &2
, &3
respectively.
Enum.map [1, 2, 3, 4], &(&1 + 1) # [2, 3, 4, 5]
sum_of_three = &(&1 + &2 + &3)
sum_of_three.(3, 2, 5) # 10
Happy reading!