Use &
to capture functions from other modules. You can use the captured functions directly as function parameters or within anonymous functions.
Enum.map(list, fn(x) -> String.capitalize(x) end)
Can be made more concise using &
:
Enum.map(list, &String.capitalize(&1))
Capturing functions without passing any arguments require you to explicitly specify its arity, e.g. &String.capitalize/1
:
defmodule Bob do
def say(message, f \\ &String.capitalize/1) do
f.(message)
end
end