【Erlang】lists:all/2 和 lists:any/2 对于空列表运算结果是啥?
先说结果, 无论F是什么, 结果都是这样:
lists:all(F, []) = true
lists:any(F, []) = false
换成其它语言也一样.
【Erlang】Erlang虚拟机的内部时钟
Erlang虚拟机可视为一个小型操作系统, 有自己的一套时钟系统, 与系统时间并不是完全一致的.
Erlang虚拟机时钟使用的是 erlang:now() 获取, 系统时间使用 os:timestamp() 获取.
格式是一样的三元组: {百万秒, 秒, 微秒}.
【Erl代码片段】start_timer/3 发送的消息中的TimerRef的用途
Erlang中有两个很相似的延迟发送消息的函数, send_after/3 和 start_timer/3,
区别仅在于前者返回Msg, 后者返回{timeout, TimerRef, Msg}.
后者的这个 TimerRef 有什么用呢?
坚强2002的博客给出了例子:
【Erl代码片段】简易的并行版lists:map/2
Joe在书上示范了一个并行化的lists:map:
pmap(F, L) ->
S = self(),
%% make_ref() returns a unique reference
%% we'll match on this later
Ref = erlang:make_ref(),
Pids = map(fun(I) ->
spawn(fun() -> do_f(S, Ref, F, I) end)
end, L),
%% gather the results
gather(Pids, Ref).
do_f(Parent, Ref, F, I) ->
Parent ! {self(), Ref, (catch F(I))}.
gather([Pid|T], Ref) ->
receive
{Pid, Ref, Ret} -> [Ret|gather(T, Ref)]
end;
gather([], _) ->
[].