Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Erlang Advanced Function (Fun)


May 13, 2021 Erlang


Table of contents


Erlang Advanced Function (Fun)

Erlang naturally has high-order functions as a functional programming language. I n the shell, we can use this:

86> Xf = fun(X) -> X * 2 end.
 #Fun<erl_eval.5.123085357>
87> Xf(5).
10

Here is a function that doubles the value and assigns it to a variable. T herefore, Xf(5) returns a value of 10. E rlang has two very useful action lists foreach and map, defined as follows:

foreach(Fun, [First|Rest]) ->
    Fun(First),
    foreach(Fun, Rest);
foreach(Fun, []) ->
    ok.

map(Fun, [First|Rest]) -> 
    [Fun(First)|map(Fun,Rest)];
map(Fun, []) -> 
    [].

These two functions are provided by the standard module lists. F oreach effects a function on each element in the list. M ap generates a new list by acting a function on each element in the list. B elow, a new list is generated Add_3 of the map in the shell:

88> Add_3 = fun(X) -> X + 3 end.
 #Fun<erl_eval.5.123085357>
89> lists:map(Add_3, [1,2,3]).
[4,5,6]

Let's output the temperature values of a group of cities again:

90> Print_City = fun({City, {X, Temp}}) -> io:format("~-15w ~w ~w~n",
[City, X, Temp]) end.
 #Fun<erl_eval.5.123085357>
91> lists:foreach(Print_City, [{moscow, {c, -10}}, {cape_town, {f, 70}},
{stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]).
moscow          c -10
cape_town       f 70
stockholm       c -4
paris           f 28
london          f 36
ok

Let's define a function that traverses the list of city temperatures and converts each temperature value to a Celsius temperature represent. H ere's what it looks like:

-module(tut13).

-export([convert_list_to_c/1]).

convert_to_c({Name, {f, Temp}}) ->
    {Name, {c, trunc((Temp - 32) * 5 / 9)}};
convert_to_c({Name, {c, Temp}}) ->
    {Name, {c, Temp}}.

convert_list_to_c(List) ->
    lists:map(fun convert_to_c/1, List).
92> tut13:convert_list_to_c([{moscow, {c, -10}}, {cape_town, {f, 70}},
{stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]).
[{moscow,{c,-10}},
 {cape_town,{c,21}},
 {stockholm,{c,-4}},
 {paris,{c,-2}},
 {london,{c,2}}]

convert_to_c is the same as before, but it is now being used as a higher-order function:

lists:map(fun convert_to_c/1, List)

When a function defined elsewhere is used as a higher-order function, we can refer to it by Function/Arity (note that Function is the function name and Arity is the number of arguments for the function). S o when you call the map function, lists:map(fun convert_to_c/1, List) A s shown above, convert_list_to_c more concise and understandable.

The lists standard library also includes the sort function sort (Fun, List), where Fun accepts two input parameters, and if the first element is smaller than the second element, the function returns true or false. A dd sorting convert_list_to_c

-module(tut13).

-export([convert_list_to_c/1]).

convert_to_c({Name, {f, Temp}}) ->
    {Name, {c, trunc((Temp - 32) * 5 / 9)}};
convert_to_c({Name, {c, Temp}}) ->
    {Name, {c, Temp}}.

convert_list_to_c(List) ->
    New_list = lists:map(fun convert_to_c/1, List),
    lists:sort(fun({_, {c, Temp1}}, {_, {c, Temp2}}) ->
                       Temp1 < Temp2 end, New_list).
93> c(tut13).
{ok,tut13}
94> tut13:convert_list_to_c([{moscow, {c, -10}}, {cape_town, {f, 70}},
{stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}]).
[{moscow,{c,-10}},
 {stockholm,{c,-4}},
 {paris,{c,-2}},
 {london,{c,2}},
 {cape_town,{c,21}}]

The following function is used in sort:

fun({_, {c, Temp1}}, {_, {c, Temp2}}) -> Temp1 < Temp2 end,

The concept of the anonymous variable """ is used here. A nonymous variables are often used in scenarios where the value of a obtained variable is ignored. O f course, it can also be used in other scenarios, not just in higher-order functions. Temp1 slt; Temp2 states that if Temp1 is smaller than Temp2, true is returned.