markOnSoftware

Anonymous Functions with Elixir

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.

Basic example

hello_world = fn -> IO.puts("Hello world") end
hello_world.() # Hello world

With arguments

multiply = fn(a, b) -> a * b end
multiply.(5, 5) # 25

Combined with pattern matching

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

Return a function

outer = fn ->
  fn(x) -> "Inner function with value: #{x}" end
end

outer.().(30) # Inner function with value: 30

Anonymous function can also access variables outside of it

fact = "I love Elixir"
fun = fn -> "Hey, #{fact}" end

fun.() # Hey, I love Elixir

Passing a function as an argument

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]

Using & 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.

Now that’s pretty basic. You can also do this…

Enum.map [1, 2, 3, 4], &(&1 + 1) # [2, 3, 4, 5]

And this…

sum_of_three = &(&1 + &2 + &3)
sum_of_three.(3, 2, 5) # 10

Happy reading!


Mark Chavez

Hi, I'm Mark Chavez. Creator of Get Things Done, yamda, public_apis, js_issues, bitcoin_index and a whole bunch of open-source projects. Join me on my adventures as I unfold the good and bad bits about software writing. You can also follow me on twitter and github for more goodies. Also, I love #oss!