<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 22 Jan 2013, at 20:11, Tyron Zerafa wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">Erlang:fun_info(F) is returning the function body because it is constructed in the shell which essentially passes the entire code tree to the erl_eval module for execution. If you call this for a function constructed in a module, you won't get this tree. <div><br></div><div>I need something along these lines that work for functions constructed in modules rather than the shell. </div></span></blockquote><div><br></div>Mind you, this is just example code - not intended for mission-critical use.<br><br></div><div><a href="http://erlang.org/pipermail/erlang-questions/2007-December/031992.html">http://erlang.org/pipermail/erlang-questions/2007-December/031992.html</a></div><div><br></div><div>I updated the program a little bit, since the internal naming of funs seems to have changed:</div><div><br></div><div><br></div><div><div>-module(extract).</div><div><br></div><div>-export([f/1]).</div><div><br></div><div><br></div><div>f(F) -></div><div> {module, Mod} = erlang:fun_info(F, module),</div><div> {name, Name} = erlang:fun_info(F, name),</div><div> {ok, Abst} = get_abstract_code(Mod),</div><div> extract_fun(Name, Abst).</div><div><br></div><div>get_abstract_code(Module) -></div><div> {module,_} = code:ensure_loaded(Module),</div><div> Beam = code:which(Module),</div><div> case beam_lib:chunks(Beam, [abstract_code]) of</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{ok,{_,[{abstract_code,{_,AC}}]}} -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> {ok, AC};</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Other -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> Other</div><div> end.</div><div><br></div><div>extract_fun(Name, AC) -></div><div> {F,Arity,Rel} = split_name(Name),</div><div> Clauses = [Cs || {function,_,F1,Arity1,Cs} <- AC,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> F1 == F, Arity1 == Arity],</div><div> Funs = pick_funs(lists:concat(Clauses)),</div><div> lists:nth(Rel, Funs).</div><div><br></div><div>split_name(Name) -></div><div> [Fs, As, _, Rs] = string:tokens(atom_to_list(Name),"/-"),</div><div> {list_to_atom(Fs), list_to_integer(As), list_to_integer(Rs)+1}.</div><div><br></div><div>pick_funs(L) -></div><div> Flatten = fun({'fun',_,_} = Fun) -> [Fun];</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (T) when is_tuple(T) -> tuple_to_list(T);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (_) -> []</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> end,</div><div> L1 = [X || X <- lists:flatten(lists:map(Flatten, L)),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> is_tuple(X)],</div><div> case [F || F <- L1,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> element(1, F) =/= 'fun'] of</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>[] -> L1;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>[_|_] -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> pick_funs(L1)</div><div> end.</div></div><div><br></div><div><br></div><div>Example:</div><div><br></div><div><div>-module(m1).</div><div><br></div><div>-export([f/2]).</div><div><br></div><div>f(N, X) -></div><div> if N==1; N==2 -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> element(N, {fun() -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> X + 1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> end,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> fun() -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> X - 1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> end});</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>N == 3 -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> fun(Y) -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> X + Y</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> end</div><div> end.</div></div><div><br></div><div><div>Eshell V5.9.2 (abort with ^G)</div><div>1> [m1:f(N,17) || N <- [1,2,3]].</div><div>[#Fun<m1.0.1800644>,#Fun<m1.1.1800644>,#Fun<m1.2.1800644>]</div><div>2> [extract:f(m1:f(N,17)) || N <- [1,2,3]].</div><div>[{'fun',7,</div><div> {clauses,[{clause,7,[],[],</div><div> [{op,8,'+',{var,8,'X'},{integer,8,1}}]}]}},</div><div> {'fun',10,</div><div> {clauses,[{clause,10,[],[],</div><div> [{op,11,'-',{var,11,'X'},{integer,11,1}}]}]}},</div><div> {'fun',14,</div><div> {clauses,[{clause,14,</div><div> [{var,14,'Y'}],</div><div> [],</div><div> [{op,15,'+',{var,15,'X'},{var,15,'Y'}}]}]}}]</div><div><br></div><div><br></div><div>The module m1 needs to be compiled with debug_info, of course.</div><div><br></div><div>BR,</div><div>Ulf W</div></div><br><div apple-content-edited="true">
<div><div>Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.</div><div><a href="http://feuerlabs.com">http://feuerlabs.com</a></div></div><div><br></div><br class="Apple-interchange-newline">
</div>
<br></body></html>