替换Erlang中的元组中的键
时间:2020-03-06 14:58:15 来源:igfitidea点击:
我有一个元组列表。 [{1,40},{2,45},{3,54} .... {7,23}],其中1 ... 7是一周中的几天(通过查找calendar:day_of_the_week()计算)。所以现在我想将列表更改为[{Mon,40},{Tue,45},{Wed,54} ... {Sun,23}]。有没有比list:keyreplace更简单的方法?
解决方案
简单的。使用httpd模块中的地图和方便的工具。
lists:map(fun({A,B}) -> {httpd_util:day(A),B} end, [{1,40},{2,45},{3,54},{7,23}]).
...或者使用其他语法:
[{httpd_util:day(A), B} || {A,B} <- L]
在哪里:
L = [{1,40},{2,45},{3,54}....{7,23}]
该构造称为列表理解,其内容为:
"Build a list of {httpd_util:day(A),B} tuples, where {A,B} is taken from the list L"