你如何从列表中走出来: ~w[dog cat sheep]
到具有整数键的地图: %{1=> "dog", 2=> "cat", 3=> "sheep"}
我的尝试:
iex(1)> list = ~w[dog cat sheep] ["dog", "cat", "sheep"] iex(2)> list |> Enum.with_index|>Enum.map(fn({a,b})->{b+1,a} end)|> Enum.into %{} %{1=> "dog", 2=> "cat", 3=> "sheep"}
有更简单的方法吗?
这是一个单行版本:
for {v, k} <- ~w[dog cat sheep] |> Enum.with_index, into: %{}, do: {k+1, v}
这与模块中的可重用函数相同:
defmodule Example do
def to_indexed_map(list, offset \\ 0)
when is_list(list)
and is_integer(offset),
do: for {v, k} <- list |> Enum.with_index,
into: %{},
do: {k+offset, v}
end
用法示例:
iex> list = ~w[dog cat sheep]
["dog", "cat", "sheep"]
iex> Example.to_indexed_map(list)
%{0 => "dog", 1 => "cat", 2 => "sheep"}
次要更新:不太简洁,但性能更高的版本(大约快2倍)如下所示.
defmodule Example do
def to_indexed_map(list, offset \\ 0)
when is_list(list)
and is_integer(offset),
do: to_indexed_map(list, offset, [])
defp to_indexed_map([], _k, acc),
do: :maps.from_list(acc)
defp to_indexed_map([v | vs], k, acc),
do: to_indexed_map(vs, k+1, [{k, v} | acc])
end