Anonymous function (fn)
Unnamed function.
Syntax
f = fn param -> expression end; f.(x)
Description
Defined with fn ... -> ... end and bound to a variable. When called, a DOT is required: f.(x). Frequently used in Enum callbacks.
Example
Loading editor…
Expected output
25
When to use?
Enum.map/each callbacks, short operations.
Common mistakes
- ⚠️ A dot is required when calling an anonymous function: square.(5).