Erlang Mailing Lists

Author Message

<  Erlang  ~  Erlang Newbie,"lists:map" like function.

Wessam
Posted: Thu Jul 31, 2008 1:01 pm Reply with quote
Joined: 31 Jul 2008 Posts: 2
Hi everyone,

I have a question that should be impossible to implement in native Erlang.

I want to be able to do something like this:

some_function(Func,[V1,V2]).

Where Func is a fun that is supplied at run time to some_function THEN some_function will do something like this:

Func(V1,V2).
(It will call Func with the list elements as its arguments).
Note that the number of elements in the list is variable.

The user will be able to use it as specified below:
foo(V1,V2,V3)->
V1+V2+V3.
start()->
Func = fun foo/3,
R=some_function(Func,[1,3,5]).
io:format("~p~n",[R]).% will print 9



This is something like "lists:map" but on all list members at once( not on a single list element at a time).

Is there a BIF to do this.


Thanks in advance:).


Last edited by Wessam on Fri Aug 01, 2008 8:34 am; edited 1 time in total
View user's profile Send private message
michal
Posted: Fri Aug 01, 2008 7:39 am Reply with quote
User Joined: 20 Jul 2006 Posts: 44 Location: London
Your some_function could look like this


Code:

some_function(Func,Args) ->
    apply(Func,Args).

_________________
http://www.erlang-consulting.com
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum