Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map function in Erlang

In addition to having the map function available with many arities (up to 4), Prolog allows you (under certain circumstances) to map a multiple arity function onto a single list. Say you want to test whether 'x' is a member of multiple lists. You can do:

maplist(member(x),[[a,b,c,x],[3,f,s,x]]).

The first argument of member is included, and the whole thing is mapped onto the list.

Question 1: Is something similar available to Erlang? I can't find it in the documentation, but then again I can't find it in any Prolog documentation either.

Question 2: How to use map (and similar functions) with multiple arities? Roll your own?

Thanks.

like image 509
Ultranewb Avatar asked Jan 18 '11 08:01

Ultranewb


People also ask

What is a map in Erlang?

A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.

What is pattern matching in Erlang?

Pattern matching occurs when evaluating a function call, case- receive- try- expressions and match operator (=) expressions. In a pattern matching, a left-hand side pattern is matched against a right-hand side term. If the matching succeeds, any unbound variables in the pattern become bound.

How do I find the length of a list in Erlang?

You can use length() to find the length of a list, and can use list comprehensions to filter your list. num(L) -> length([X || X <- L, X < 1]).


1 Answers

Maybe something like:

23> lists:map(fun(L) -> lists:member(42,L) end, [[1,5,8,4],[13,42],[7,2,10]]).
[false,true,false]
24>
like image 79
YasirA Avatar answered Sep 20 '22 04:09

YasirA