[erlang-questions] Convert atom and arity into fun

Robert Virding rvirding@REDACTED
Fri Sep 5 12:43:55 CEST 2008


Of course another way of doing it which seems more in accord to what was
asked, at least how I interpreted it, is to do:

    F = fun mod:name/arity,        % or fun name/arity if local function
    F(Arg1, Arg2, ...)

if you know the name and arity of the function. This will not work if you
don't know the name or arity of the function at compile-time, then use
Gleb's suggestion.

Robert

2008/9/5 Gleb Peregud <gleber.p@REDACTED>

> On Fri, Sep 5, 2008 at 12:22 PM, Jeroen Koops <koops.j@REDACTED> wrote:
> > Hi,
> >
> > The answer to this question is probably obvious, but I can't find it
> > after searching around for a while:
> >
> > Given a function's name as an atom and an arity (for instance as
> > returned by module_info(exports)), how can I convert this into a fun
> > that I can invoke?
> >
> > Thanks in advance,
> >
> > Jeroen Koops
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
> Check out erlang:apply/3 (it's a BIF). It is described in manual (man
> erlang).
>
> You can use Module:Function(Arg1, Arg2, ...) where Module and Function
> are variables bound to appropriate atoms. Like this:
>
> Module = lists,
> Function = flatlength,
> [1, 2] = Module:Function([1], [2])
>
>
> --
> Gleb Peregud
> http://gleber.pl/
>
> Every minute is to be grasped.
> Time waits for nobody.
> -- Inscription on a Zen Gong
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080905/9d7a16be/attachment.htm>


More information about the erlang-questions mailing list