erlang学习笔记(一)
?
1.lists:append(List1, List2) -> List1 ++ List2. ?左边的list要复制,递归时要注意。
? lists:flatten/1 将嵌套的list变成不嵌套,当只有一个层级的时候,用lists:append/1
?
2. if、when 的表达式异常会被内部吞掉
?
3.快速排序: 取一个基数,比它小的放左面,比它大的放右面,然后重复递归。
? qSort([H|T]) ->
qSort([X || X <- T, X < H ]) ++ [H] ++ qSort([Y || Y <- T, Y > H]);
? qSort([]) ->
[].
?
4.lists:reverse() 片段
? reverse([] = L) ->
? ? L;
? reverse([_] = L) ->
? ? L;
? reverse([A, B]) ->
? ? [B, A];
? reverse([A, B | L]) ->
? ? lists:reverse(L, [B, A]).
?
5.用re模块替换正则表达式
?
6.使用erlang:send_after/3、erlang:start_timer/3 比用timer模块效率更高。
? ? timer模块的使用一条独立的线程来管理所有定时器,如果太多定时或者频繁取消定时,就会影响性 ?
?
? ?能。(timer:tc/3 or timer:sleep/1 除外)
? ? send_after如果是给注册名进程发送,就算进程不存在,都不会产生异常,进程消失,也不会自 ? ? ?
?
动取消定时;相反,如果是给PID发送,当进程不存活的时候会自动取消定时。
? ? 取消定时用erlang:cancale_time/1
?
7.send_after/3 发送的是Msg,start_timer/3发送的是{timeout,TimeRef,Msg}.
?
8.atom不会被GC回收,所以尽量不要用list_to_atom,或者循环产生大量atom, 默认1048576就会爆掉
?
9.length/1效率是N,不是常数,用tuple_size/1, byte_size/1代替size/1 会更容易发现问题和性能更
?
好
?
10.用位模式匹配 代替split_binary/2
?
11. 当2个list很大时, -- 操作很慢。
? ? 不要 HugeList1 -- HugeList2
? ? 应该?
HugeSet1 = ordsets:from_list(HugeList1),
? ? ? ? HugeSet2 = ordsets:from_list(HugeList2),
? ? ? ? ordsets:subtract(HugeSet1, HugeSet2)
?
? ? 如果顺序很重要的话,可以
Set = gb_sets:from_list(HugeList2),
? ? ? ? [E || E <- HugeList1, not gb_sets:is_element(E, Set)]
?
?
12.erlang最大同时存活线程数默认是32768,系统启动时可以通过 +p 最大设置为 268435456?
?
13.atom中的字符最大长度255
?
14.默认最大的ets table数量是1400, 可以通过 ERL_MAX_ETS_TABLES 修改
?
15.方法最多可以有255个参数
?
16.循环N次执行某个函数F: lists:foreach(fun(_)-> F() end,lists:duplicate(N,dummy)).
?
17.simple_one_for_one 特点:
? ? ?1.simple_one_for_one内部保存child是使用dict,而其他策略是使用list,因此 ? ? ??
?
simple_one_for_one更适合child频繁创建销毁、需要大量child进程的情况,具体来说例如网络连接的
?
频繁接入断开。
? ? ?2.使用了simple_one_for_one后,无法调用terminate_child/2 delete_child/2 restart_child/2?
? ? ?3.start_child/2 对于simple_one_for_one来说,不必传入完整的child spect,传入参数list,会自
?
动进行参数合并。在一个地方定义好child spec之后,其他地方只要start_child传入参数即可启动child
?
进程,简化child都是同一类型进程情况下的编程。
?
18.prim_inet 模块
?
19.io_lib:format(<<"update `player` set `last_login_time` = ~p, `online_flag`=1 where ? ? ?
?
id=~p">>,[Time, Id])
?
20.用atom标识方法
? ? ?例如:?
validate_name(Name) ?->
? ?validate_name(len, Name).
?
validate_name(len, Name) ->
? ? case asn1rt:utf8_binary_to_list(list_to_binary(Name)) of
? ? ? ? {ok, CharList} ->
? ? ? ? ? ? Len = string_width(CharList), ??
? ? ? ? ? ? case Len < 11 andalso Len > 1 of
? ? ? ? ? ? ? ? true ->
? ? ? ? ? ? ? ? ? ? validate_name(existed, Name);
? ? ? ? ? ? ? ? false ->
? ? ? ? ? ? ? ? ? ? %%角色名称长度为1~5个汉字
? ? ? ? ? ? ? ? ? ? {false, 5}
? ? ? ? ? ? end;
? ? ? ? {error, _Reason} ->
? ? ? ? ? ? %%非法字符
? ? ? ? ? ? {false, 4}
? ? end;?
?
validate_name(existed, Name) ->
? ? case lib_player:is_exists(Name) of
? ? ? ? true ->
? ? ? ? ? ? %角色名称已经被使用
? ? ? ? ? ? {false, 3}; ? ?
? ? ? ? false ->
? ? ? ? ? ? true
? ? end;
?
validate_name(_, _Name) ->
? ? {false, 2}.
?
%% 字符宽度,1汉字=2单位长度,1数字字母=1单位长度
string_width(String) ->
? ? string_width(String, 0).
string_width([], Len) ->
? ? Len;
string_width([H | T], Len) ->
? ? case H > 255 of
? ? ? ? true ->
? ? ? ? ? ? string_width(T, Len + 2);
? ? ? ? false ->
? ? ? ? ? ? string_width(T, Len + 1)
? ? end.
?
21.element(N, Tuple) 获取元组指定位置的元素
?
22.nth(N, List) 获取第N个list元素
?
23.ets相对进程字典的优点:
? ? ? 1.其它进程可以共享
? ? ? 2.ets提供了查找,模糊查询,迭代等方法,而进程字典只是一个简单的key-value存储
? ? ? 3.可以保存数据到文件
? ? ? 4.如果拥有者进程挂了,ets可以由继承进程拥有