<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div style="-webkit-text-size-adjust: auto; ">Ulf,</div><div style="-webkit-text-size-adjust: auto; "><br></div><div style="-webkit-text-size-adjust: auto; ">How do I reconcile your answer with the previous reference to: <a class="moz-txt-link-freetext" href="http://www.erlang.org/doc/efficiency_guide/functions.html#id67199">http://www.erlang.org/doc/efficiency_guide/functions.html#id67199</a>  which seems to imply that Funs are significantly slower?</div><div><br></div><div><br></div><div style="-webkit-text-size-adjust: auto; ">On Sep 23, 2012, at 8:55 AM, Ulf Wiger <<a href="mailto:ulf@feuerlabs.com">ulf@feuerlabs.com</a>> wrote:<br><br></div><div style="-webkit-text-size-adjust: auto; "><span></span></div><blockquote type="cite" style="-webkit-text-size-adjust: auto; "><div><div><br></div><div>To begin with, your example won't compile, but the question is still valid.</div><div><br></div><div>Consider this program:</div><div><br></div><div>=================</div><div><div>-module(foldtest).</div><div>-export([f1/2, f2/2]).</div><div><br></div><div>f1(Acc, Lst) -></div><div>    F = fun(A,B) -> my_function(A,B) end,</div><div>    lists:foldl(F, Acc, Lst).</div><div><br></div><div>f2(Acc, Lst) -></div><div>    lists:foldl(fun my_function/2, Acc, Lst).</div><div><br></div><div>my_function(X, Acc) -></div><div>    [X|Acc].</div></div><div><div>=================</div><div></div></div><div><br></div><div>I believe this illustrates the question, but compiles and runs correctly.</div><div><br></div><div><div>Eshell V5.9  (abort with ^G)</div><div>1> foldtest:f1([], [1,2,3]).</div><div>[3,2,1]</div><div>2> foldtest:f2([], [1,2,3]).</div><div>[3,2,1]</div></div><div><br></div><div>Now, let's compile the code with `erlc -S foldtest.erl` (generating ASM code):</div><div><br></div><div><div>{function, f1, 2, 2}.</div><div>  {label,1}.</div><div>    {line,[{location,"foldtest.erl",4}]}.</div><div>    {func_info,{atom,foldtest},{atom,f1},2}.</div><div>  {label,2}.</div><div>    {allocate,2,2}.</div><div>    {move,{x,1},{y,0}}.</div><div>    {move,{x,0},{y,1}}.</div><div>    {make_fun2,{f,14},0,0,0}.</div><div>    {move,{y,0},{x,2}}.</div><div>    {move,{y,1},{x,1}}.</div><div>    {line,[{location,"foldtest.erl",6}]}.</div><div>    {call_ext_last,3,{extfunc,lists,foldl,3},2}.</div><div><br></div><div><br></div><div>{function, f2, 2, 4}.</div><div>  {label,3}.</div><div>    {line,[{location,"foldtest.erl",9}]}.</div><div>    {func_info,{atom,foldtest},{atom,f2},2}.</div><div>  {label,4}.</div><div>    {allocate,2,2}.</div><div>    {move,{x,1},{y,0}}.</div><div>    {move,{x,0},{y,1}}.</div><div>    {make_fun2,{f,12},0,0,0}.</div><div>    {move,{y,0},{x,2}}.</div><div>    {move,{y,1},{x,1}}.</div><div>    {line,[{location,"foldtest.erl",10}]}.</div><div>    {call_ext_last,3,{extfunc,lists,foldl,3},2}.</div></div><div><br></div><div>As you can tell, the two alternatives generate identical code.</div><div><br></div><div>The fun is compiled into a regular function, albeit with a funny name, and calling it carries the same cost as calling a hand-written function. Actually instantiating a fun context carries a slight overhead depending on the number of variables that need to be imported into the context (in this case, zero). There will be one level of indirection in both cases.</div><div><br></div><div>BR,</div><div>Ulf W</div><br><div><div>On 23 Sep 2012, at 02:07, James Rosenblum wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; ">Is there an appreciable difference in these two ways of passing a function?</div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; ">F = fun(A,B) -> my_function(A,B) end.</div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; ">lists:map(F, Lst)</div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; ">vs.</div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; ">-export([my_function/2]).</div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 19px; font-family: arial, helvetica, sans-serif; background-color: transparent; font-style: normal; ">lists:map(fun my_function/2, Lst)</div></blockquote></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></div></blockquote></div></body></html>