Anonymous functions

These store and pass executable functions around as if it was an integer or a string. They start with fn and end with end

Setting the anonymous function

variable_that_holds_the_function = fn variable1, variable2, variable3 -> variable1 + variable2 + variable3 end

with Capture Operator & as Shortcut

variable_that_holds_the_function = &(&variable1 + &variable2 + &variable3)

Calling the anonymous function

variable_that_holds_the_function.(data1, data, 2, data3)

Static variables set within the anoynmous function will reference that static variable in memory.

add = fn a, b -> a + b end
#Function<12.71889879/2 in :erl_eval.expr/5>

add.(1, 2)
3

is_function(add)
true

The anonymous function is fn.

  • It gets 2 arguments: a and b
  • It returns the result of a + b

fn is stored in the variable add.

A dot between the variable and parentheses is required to invoke an anonymous function.

  • It prevents add/1 or add/2

Anonymous Functions vs Named Functions

Anonymous Functions are identified by the number of arguments they get.

A function can be checked for arity via is_function/2:

Check if add is a function that expects exactly 2 arguments

is_function(add, 2)
true

Check if add is a function that expects exactly 1 argument

is_function(add, 1)
false

Anonymous functions can also access variables that are in scope when the function is defined.

This is typically referred to as closures, as they close over their scope.

add = fn a, b -> a + b end

double = fn a -> add.(a, a) end
#Function<6.71889879/1 in :erl_eval.expr/5>

double.(2)
4

A variable assigned inside a function does not affect its surrounding environment.

x = 42

(fn -> x = 0 end).()
0

x
42