[erlang-questions] how: to serialize a function?

Renato Lucindo lucindo@REDACTED
Mon Feb 25 21:51:38 CET 2008


1> F = fun(X) -> X * X end.
#Fun<erl_eval.6.35866844>
2> B = term_to_binary(F).
<<131,112,0,0,2,177,1,193,97,132,164,217,241,72,144,2,77,
 131,144,112,241,112,21,0,0,0,2,0,0,...>>
%%% you can send the binary to other processes
3> apply(F, [2]).
4
4> apply(binary_to_term(B), [2]).
4
5>

[]'s

Lucindo

On Mon, Feb 25, 2008 at 5:35 PM, Tim Fletcher <twoggle@REDACTED> wrote:
> Given an example function like so:
>
>     > F = fun(X) -> X * X end.
>
>  My first question is: how can I serialize (and de-serialize) that
>  function completely (i.e. including its args)? I know that it's easy
>  to send that function between processes/nodes, but how do I replicate
>  that serialization myself?
>
>  Next, given that I can get a string representation of the function like so:
>
>     > erlang:fun_to_list(F).
>     "#Fun<erl_eval.6.49591080>"
>
>  My second question is: what function do I use to turn that string back
>  into (a proper "reference" to) the function F?
>  _______________________________________________
>  erlang-questions mailing list
>  erlang-questions@REDACTED
>  http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list