[erlang-questions] passing a function to lists:map
Raimo Niskanen
raimo+erlang-questions@REDACTED
Mon Mar 22 13:34:10 CET 2010
On Mon, Mar 22, 2010 at 01:01:04PM +0100, Jeroen Koops wrote:
> Really now? That goes to show, teaching =:= learning !
>
> On Mon, Mar 22, 2010 at 12:57 PM, Bengt Kleberg
> <bengt.kleberg@REDACTED>wrote:
>
> > Greetings,
> >
> > You do not need to export the function to be able to use it in a
> > fun func/arity
> > expression.
The export is not needed since the "fun Fun/1"
construct is essentially syntactical sugar for
"fun (Arg) -> Fun(Arg) end" like this:
atomize(List) -> lists:map(fun(Arg) -> ta(Arg) end, List).
So you could do a macro approach to replace "fun Fun/1":
-define(FUN1(Fun), fun (Arg) -> (Fun)(Arg) end).
atomize(List) -> lists:map(?FUN1(ta), List).
> >
> >
> > ebngt
> >
> > On Mon, 2010-03-22 at 12:53 +0100, Jeroen Koops wrote:
> > > Hi Martin,
> > >
> > > use:
> > >
> > > -export([ ta/1 ]).
> > >
> > > and then:
> > >
> > > atomize(List) -> lists:map(fun ta/1, List).
> > >
> > > Regards,
> > >
> > >
> > >
> > > On Mon, Mar 22, 2010 at 12:49 PM, Martin DeMello <
> > martindemello@REDACTED>wrote:
> > >
> > > > ta(X) when is_list(X) -> list_to_atom(X);
> > > > ta(X) when is_tuple(X) -> tuple_to_list(X);
> > > > ta(X) when is_integer(X) -> X.
> > > >
> > > > % this doesn't work
> > > > atomize(List) -> lists:map(ta, List).
> > > >
> > > > % this does
> > > > atomize(List) -> lists:map(fun(X) -> ta(X) end, List).
> > > >
> > > > What's the difference? Have I just got the syntax wrong, or is there
> > > > some fundamental reason I need the second version?
> > > >
> > > > martin
> > > >
> > > > ________________________________________________________________
> > > > erlang-questions (at) erlang.org mailing list.
> > > > See http://www.erlang.org/faq.html
> > > > To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> > > >
> > > >
> >
> >
> > ________________________________________________________________
> > erlang-questions (at) erlang.org mailing list.
> > See http://www.erlang.org/faq.html
> > To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> >
> >
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list