[Erlang Programming Book] Using map/2 with a named function

73 views
Skip to first unread message

Ivan Uemlianin

unread,
May 10, 2010, 12:13:33 PM5/10/10
to Erlang Programming
Dear All

Using map wasn't as straightforward as I thought it would be. Running
this script:

-module(maptest).
-export([test/0]).
-export([double/1]).

double(N) ->
2 * N.

test() ->
List = [1,2,3,4,5],
lists:map(double, List).

Raises the error:

12> c(maptest).
{ok,maptest}
13> maptest:test().
** exception error: bad function double
in function lists:map/2

I found an answer on a blog [1]. This works:

-module(maptest).
-export([test/0]).
%% -export([double/1]). %% not necessary

double(N) ->
2 * N.

test() ->
List = [1,2,3,4,5],
lists:map(fun double/1, List).

So, to use a named (i.e., rather than anonymous) function in map, it
seems necessary to use fun module:function/arity. Is that true? I
can't find this documented either in the book or on the Erlang
website. Surely [1] is not the definitive source?

Best

Ivan


[1] http://thraxil.org/users/anders/posts/2006/08/21/Erlang-Challenge-Level-2/

--
Erlang Programming Website:
http://www.erlangprogramming.org/

Ale

unread,
May 10, 2010, 12:24:53 PM5/10/10
to erlang-prog...@googlegroups.com


2010/5/10 Ivan Uemlianin <iv...@llaisdy.com>
It is true. How would you differenciate the function 'double' from the atom 'double'?

It's annoying... I know. I asked in #erlang in freenode irc channel.
 
I
can't find this documented either in the book or on the Erlang
website.  Surely [1] is not the definitive source?

Best

Ivan


[1]  http://thraxil.org/users/anders/posts/2006/08/21/Erlang-Challenge-Level-2/

--
Erlang Programming Website:
http://www.erlangprogramming.org/



--
Ale.

Ivan Uemlianin

unread,
May 10, 2010, 12:31:56 PM5/10/10
to Erlang Programming
> > So, to use a named (i.e., rather than anonymous) function in map, it
> > seems necessary to use fun module:function/arity.  Is that true?
>
> It is true. How would you differenciate the function 'double' from the atom
> 'double'?

Ah yes. That must be the point. Thanks.

Ivan

Francesco Cesarini (Erlang Solutions)

unread,
May 10, 2010, 12:41:39 PM5/10/10
to erlang-prog...@googlegroups.com
In the older releases of Erlang, {Module, Function} was also treated as
a fun. While depriciated, it still works for backward compatibility reasons:

1> Fun = {io, format}, Fun("Hello~n").
Hello
ok
2>

Francesco


On 10/05/2010 17:31, Ivan Uemlianin wrote:
>>> So, to use a named (i.e., rather than anonymous) function in map, it
>>> seems necessary to use fun module:function/arity. Is that true?
>>>
>> It is true. How would you differenciate the function 'double' from the atom
>> 'double'?
>>
> Ah yes. That must be the point. Thanks.
>
> Ivan
>
>
---------------------------------------------------

---------------------------------------------------

WE'VE CHANGED NAMES!

Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.

www.erlang-solutions.com
Reply all
Reply to author
Forward
0 new messages