Funs: anonymous functions
fun ... end
The fun keyword starts an anonymous function, and end closes it. The arguments sit in parentheses, and the body follows the arrow.
A fun is a value like any other: bind it, pass it, or return it. This module returns one from a named function. Save it as funs.erl.
-module(funs).
-export([double/0, adder/1, twice/2, inc/1]).
double() ->
fun(X) -> X * 2 end.
adder(N) ->
fun(X) -> X + N end.
twice(F, X) ->
F(F(X)).
inc(X) ->
X + 1.
erlc funs.erl
erl
1> F = funs:double(), F(21).
42
2> is_function(F).
true
Apply a fun
Call a fun directly with parentheses. The erlang:apply function does the same with the fun and its arguments as a list.
Direct calls are the everyday form; apply shows up when the argument list is built at runtime.
erl
1> Add = fun(A, B) -> A + B end, erlang:apply(Add, [2, 3]).
5
2> is_function(Add, 2).
true
Capture by name
The fun with a module, function, and arity captures an existing function without calling it. The result is a fun that calls that function.
This is the bridge between named functions and the higher-order tools of lesson 22.
erl
1> Member = fun lists:member/2.
fun lists:member/2
2> Member(2, [1, 2, 3]).
true
3> lists:map(fun funs:inc/1, [1, 2]).
[2,3]
Closures
A fun remembers the variables around it at the moment it was created. The captured values stay alive inside the fun, even after the outer function returns.
Closures keep state without mutation: the captured value is bound once, as ever.
erl
1> Add10 = funs:adder(10), Add10(5).
15
2> N = 10, AddN = fun(X) -> X + N end, AddN(5).
15
3> funs:twice(fun funs:inc/1, 40).
42
Next: Higher-order functions
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the
funkeyword build?Answer
An anonymous function
fun ... endis a function literal: a value with arguments and a body, and no name of its own. -
How do you call the fun bound to
Fwith the argument21?Answer
F(21)A bound fun is applied with parentheses, exactly like a named function.
-
What does
is_function(F, 2)check?Answer
That
Fis a fun of arity 2The second argument is the arity: the number of arguments the fun takes.
-
What does
fun lists:member/2do?Answer
Builds a fun that calls
lists:member/2The module, function and arity capture a named function as a fun; nothing runs until the fun is called.
-
What is
erlang:apply(F, [2, 3])equivalent to?Answer
F(2, 3)applytakes the fun and the argument list, and calls the fun with those arguments. -
What is a closure?
Answer
A fun that captures variables from its surroundings
The fun carries the values it saw when it was created, and uses them on every call.
-
In
Add10 = funs:adder(10), where does the captured value come from?Answer
From the argument
Nofadder/1adder/1returns a fun that adds its own argument to theNit was called with. -
Can a fun change a variable of the function that created it?
Answer
No: values are immutable, the fun only reads them
Erlang has single assignment, so the captured value is fixed; a change means a new binding, not a mutation.
-
What does
funs:double()return?Answer
A fun of arity 1
double/0returnsfun(X) -> X * 2 end, a fun that takes one argument. -
What does
funs:twice(fun funs:inc/1, 40)return?Answer
42twice/2applies the fun twice:40 + 1then41 + 1.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev