[erlang-questions] Efficiency is passing a functin

Richard O'Keefe <>
Sun Sep 23 23:56:32 CEST 2012


On 23/09/2012, at 12:07 PM, James Rosenblum wrote:
> Is there an appreciable difference in these two ways of passing a function?
> 
> F = fun(A,B) -> my_function(A,B) end.
> lists:map(F, Lst)
> 
> vs
> 
> -export([my_function/2]).
> 
> lists:map(fun my_function/2, Lst)

The two are not precisely semantically equivalent.

- The second one exposes a function to external use;
  you should generally expose nothing that you don't mean to retain.
+ The second one will work nicely if you hot-reload the module.

But there are two questions I have to ask:

(1) Why not write [my_function(X) || X <- Lst]
    and get a direct local call to my_function without exporting
    anything extra?

(2) Why not *measure* the three versions and see whether the
    difference is of any practical significance in the context
    of everything else going on in the real program?





More information about the erlang-questions mailing list