{p,zen,23}
Eshell V5.8.2 (abort with ^G)
1> list_to_tuple([a,b,c]).
{a,b,c}
2> tuple_to_list({a,b,c}).
[a,b,c]
3>
4> F={lists,append}.
{lists,append}
5> F([1,2],[3,4]).
[1,2,3,4]
6> fun lists:append/2([a,b],[c,d]).
[a,b,c,d]
7>
Myth: Funs are slow http://www.erlang.org/doc/efficiency_guide/myths.html
Yes, funs used to be slow. Very slow. Slower than apply/3. Originally, funs were implemented using nothing more than compiler trickery, ordinary tuples, apply/3, and a great deal of ingenuity.
But that is ancient history. Funs was given its own data type in the R6B release and was further optimized in the R7B release.
Now the cost for a fun call falls roughly between the cost for a call to local function and apply/3.
Warning:
Tuples are not fun(s). A "tuple fun", {Module,Function}, is not a fun. The cost for calling a "tuple fun" is similar to that of apply/3 or worse. Using "tuple funs" is strongly discouraged, as they may not be supported in a future release, and because there exists a superior alternative since the R10B release, namely the fun Module:Function/Arity syntax.
%%% 版本一
-module(a).
-compile(export_all).
meta()->
this_is_version_10001.
r()->
receive
code_switch -> a:r();
Msg-> io:format("Msg:~p~n",[Msg]),r()
end.
%%% 版本二
-module(a).
-compile(export_all).
meta()->
this_is_version_2012.
r()->
receive
code_switch -> a:r();
Msg-> io:format("Now Msg:~p~n",[Msg]),r()
end.
Eshell V5.9 (abort with ^G)
1> a:meta().
this_is_version_2012
2> P= spawn(a,r,[]).
<0.35.0>
3> P!abc.
Now Msg:abc
abc
4> c:l(a).
{module,a}
5> P2= spawn(a,r,[]).
<0.39.0>
6> a:meta().
this_is_version_10001
7> P2!abc.
Msg:abc
abc
8> P!code_switch.
code_switch
9> P!abc.
Msg:abc
abc
10>
loop(Fun, State) ->
receive
{From, {rpc, Tag, Q}} ->
{Reply, State1} = Fun(Q, State),
From ! {Tag, Reply},
loop(Fun, State1);
{code_upgrade, Fun1} ->
loop(Fun1, State)
end.
相关: Live Code Update in Erlang