Replace keys in Erlang motorcade

I have a list of tuples, for example. [{1.40}, {2.45}, {3.54} .... {7.23}], where 1 ... 7 are days of the week (calculated by finding the calendar: day_of_the_week ()). So now I want to change the list to [{Mon, 40}, {Tue, 45}, {Wed, 54} ... {Sun, 23}]. Is there an easier way to do this than lists: keyreplace?

+7
erlang tuples
source share
2 answers

Simple Use the map and the handy tool from the httpd module.

lists:map(fun({A,B}) -> {httpd_util:day(A),B} end, [{1,40},{2,45},{3,54},{7,23}]). 
+3
source share

... or using another syntax:

 [{httpd_util:day(A), B} || {A,B} <- L] 

Where:

 L = [{1,40},{2,45},{3,54}....{7,23}] 

The construct is called list comprehension and reads as:

"Create a list of tuples {httpd_util:day(A),B} , where {A,B} is taken from list L "

+13
source share

All Articles