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

Renato Lucindo lucindo@REDACTED
Mon Feb 25 22:52:18 CET 2008


With some limitations its possible to use something like this:

Eshell V5.6  (abort with ^G)
1> EvalStr = fun(Str) ->
1> {ok, Tokens, _} = erl_scan:string(Str),
1> {ok, [Form]} = erl_parse:parse_exprs(Tokens),
1> {value, Result, _} = erl_eval:expr(Form, erl_eval:new_bindings()),
1> Result
1> end.
#Fun<erl_eval.6.35866844>
2> FStr = "fun(X) -> X * X end.".
"fun(X) -> X * X end."
3> F = EvalStr(FStr).
#Fun<erl_eval.6.35866844>
4> F(4).
16
5> GStr = "fun(X) -> A = 10, X * A end.".
"fun(X) -> A = 10, X * A end."
6> G = EvalStr(GStr).
#Fun<erl_eval.6.35866844>
7> G(4).
40
8>

[]'s

Lucindo

On Mon, Feb 25, 2008 at 6:33 PM, Matthew Dempsky <matthew@REDACTED> wrote:
> On 2/25/08, Tim Fletcher <twoggle@REDACTED> wrote:
>
> > Just checking - I wasn't sure whether it was something that could
>  >  somehow be "looked up" in the process that defined the function.
>
>  Nope, two different funs at the same place with different bound
>  variables will have the same string representation, even though
>  they're different terms:
>
>  1> A = fun() -> true end.
>  #Fun<erl_eval.20.67289768>
>  2> B = fun() -> false end.
>  #Fun<erl_eval.20.67289768>
>  3> A == B.
>  false
>
>
> _______________________________________________
>  erlang-questions mailing list
>  erlang-questions@REDACTED
>  http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list