[erlang-questions] How to wrap a function of any arity

Ulf Wiger (TN/EAB) <>
Fri Nov 17 17:23:35 CET 2006

Instead of passing the function name, pass a fun:
Log_compute = wrap_fun(fun() -> compute(AllArgs) end, logger).
wrap_fun(Fun, Pid) ->
    Res = Fun(),
    Pid ! Res.
The fun acts as a "closure" and inherits the state from its
It can fully encapsulate the data needed, so there is no need for any 
(The example now feels a bit contrived, but perhaps you get the point
Ulf W


[mailto:] On Behalf Of Ludovic
	Sent: den 17 november 2006 16:47
	Subject: [erlang-questions] How to wrap a function of any arity
	I would like to wrap a function into another which send the the
result of the inner function to some external process.
	Let say I have a function called "compute" and a log process
"logger": I want to create a new function with something like: 
	Log_compute = wrap_fun(compute, logger).
	wrap_fun(Fun, Pid) ->
	    fun(AllArgs) ->
	        Res = Fun(AllArgs),
	        Pid ! Res
	But I don't know how to do that with a function of any arity
(the code above work only for function of arity 1).
	Is this do-able? if not, how should I do?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20061117/1bfa7a71/attachment.html>

More information about the erlang-questions mailing list