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

Ludovic Coquelle lcoquelle@REDACTED
Fri Nov 17 17:32:40 CET 2006


Ok, that's what I do for now, but it doesn't really fit my need: I do not
know the arguments at the time to wrap the function.
I wanted to first wrap the function and then apply the resulting wrapped
function with different arguments.

The idea was to propose an API which get any function from an external
module, and transform it (wrap) in another kind of function that I can use
in my framework.
Anyway, if it is not possible I will revise my design.
Thanks

On 11/18/06, Ulf Wiger (TN/EAB) <ulf.wiger@REDACTED> wrote:
>
>
> 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/20061118/5614130d/attachment.htm>


More information about the erlang-questions mailing list