我有List
Tuples
:
golden = [{%{"age" => 80, "name" => "Dorothy"},
%{"age" => 50, "name" => "Rose"}},
{%{"age" => 90, "name" => "Sophia"},
%{"age" => 60, "name" => "Blanche"}}]
方法#1
我使用Enum.map
如下,但收到错误:
Enum.map(golden, Tuple.to_list)
** (UndefinedFunctionError) undefined function Tuple.to_list/0
(elixir) Tuple.to_list()
方法#2
当我定义匿名函数时:
f = fn(data) -> Tuple.to_list(data) end
并使用Enum.map
,我得到了理想的结果:
Enum.map(golden, f)
[[%{"age" => 80, "name" => "Dorothy"}, %{"age" => 50, "name" => "Rose"}],
[%{"age" => 90, "name" => "Sophia"}, %{"age" => 60, "name" => "Blanche"}]]
也许我错过了一些东西,但这两种方法不完全相同吗?
答案 0 :(得分:3)
您遗失& operator
Capture operator:
捕获或创建匿名函数。
iex()> Enum.map(golden, &Tuple.to_list/1)
[[%{"age" => 80, "name" => "Dorothy"}, %{"age" => 50, "name" => "Rose"}],
[%{"age" => 90, "name" => "Sophia"}, %{"age" => 60, "name" => "Blanche"}]]