Erlang 高阶函数 (Fun)

Erlang 作为函数式编程语言自然拥有高阶函数。

1. 例如定义一个数值翻倍的函数如下:

%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").

%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
  Xf = fun(X) -> X * 2 end,
  io:format("The result is : ~p   ~n", [Xf(6)]).

运行结果:

2. Erlang 有两个非常有用的操作列表的函数 foreach 与 map, 定义如下:

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

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

这两个函数是由标准模块 lists 提供的。foreach 将一个函数作用于列表中的每一个元素。 map 通过将一个函数作用于列表中的每个元素生成一个新的列表。

%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").

%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
  Xf = fun(X) -> X * 2 end,
  Add_3 = fun(X) -> X + 3 end,
  io:format("The result is : ~p   ~n", [lists:map(Add_3, [1,2,3])]).

运行结果:

输出一组城市的温度:

%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").

%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
  Print_City = fun({City, {X, Temp}}) -> io:format("~-15w ~w ~w~n", [City, X, Temp]) end,
  io:format("The result is : ~p   ~n", [lists:foreach(Print_City, [{moscow, {c, -10}}, {cape_town, {f, 70}}, {stockholm, {c, -4}},
    {paris, {f, 28}}, {london, {f, 36}}])]).

运行结果:

下面定义一个函数,这个函数用于遍历城市温度列表并将每个温度值都转换为摄氏温度表示:

%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").

%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
  io:format("The result is : ~p   ~n", [convert_list_to_c([{moscow, {c, -10}}, {cape_town, {f, 70}},
    {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}])]).

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).

运行结果:

lists 标准库中还包括排序函数 sort(Fun,List),其中 Fun 接受两个输入参数,如果第一个元素比第二个元素小则函数返回真,否则返回假。

%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 一月 2019 上午 10:55
%%%-------------------------------------------------------------------
-module(helloworld).
-author("Administrator").

%% API
-export([start/0]).
%%-export([format_temps/1]).
start() ->
  io:format("The result is : ~p   ~n", [convert_list_to_c([{moscow, {c, -10}}, {cape_town, {f, 70}},
    {stockholm, {c, -4}}, {paris, {f, 28}}, {london, {f, 36}}])]).


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).

运行结果:

这儿用到了匿名变量 "_" 的概念。匿名变量常用于忽略一个获得的变量值的场景下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值