You can define anonymous functions using fn
:
iex> multiply = fn (x,y) -> x + y end
iex> multiply.(2,3)
6
You can use &
for shorthand form, where &N
refers to the N
-th parameter:
iex> multiply = &(&1 * &2)
iex> multiply.(2,3)
6
Functions can have multiple clauses using pattern matching:
fn
{:ok, result} -> "Success: #{result}"
{_, error} -> "Error: #{error}"
end