[集]erlang常用函数收集
1、消重算法:
1> List = [1,2,3,1,2,4]. [1,2,3,1,2,4] 2> sets:to_list(sets:from_list(List)). [3,2,1,4]
2、排序算法:
3>lists:sort(List). [1,1,2,2,3,4]
根据某一元素排序
4>rd(app{id,date,pkg_name,appname,version,user_num,new_user_num,used_num,los,sum_user}). app 5> Sort = fun (A,B)-> A#app.date =< B#app.date end. #Fun<erl_eval.12.113037538> 6> lists:sort(Sort, D).%按记录中的date字段进行排列 [#app{ id = {app_id,{2010,7,1},<<"Packagename">>}, date = {2010,7,1}, pkg_name = <<"Packagename">>,appname = <<"Appname">>, version = <<"Version">>,user_num = 1,new_user_num = 1, used_num = 1,los = 7223,sum_user = 2}... ...
3、排序+消重算法:
7> ordsets:from_list(List). [1,2,3,4]
4、
foldl(Fun(Elem, AccIn), Acc0, List) -> Acc1 %%AccIn初使为Acc0, Elem是List中的值,把List中的所有值都执行函数一次得到列表 lists:foldl(fun(Key, Acc) ->[proplists:get_value(Key, Options) | Acc] end, [], lists:reverse([pool_id, hostname, port, username,password,database, logfun, encoding, poolsize])) 注:这儿的Options="[{hostname,"localhost"}, {username, "root"}, {password,"sa"}, {database, "test"}]" 结果为[undefine,"localhost", undefine,"root","sa","test", undefine, undefine, undefine]