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

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
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
surroundings.
It can fully encapsulate the data needed, so there is no need for any 
arguments.
 
(The example now feels a bit contrived, but perhaps you get the point
anyway?)
 
BR,
Ulf W


________________________________

	From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Ludovic
Coquelle
	Sent: den 17 november 2006 16:47
	To: erlang-questions@REDACTED
	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
	    end.
	
	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.htm>


More information about the erlang-questions mailing list