Elixir:将列表转换为具有整数键的映射
Elixir: Convert a list into a map with integer keys
你如何从列表中选择:~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
["dog", "cat", "sheep"]
|> Enum.with_index(1)
|> Enum.reduce(%{}, fn {item, index}, acc -> Map.put(acc, index, item) end)
returns
%{1 => "dog", 2 => "cat", 3 => "sheep"}
此处使用 Enum.with_index(1) 以索引 1 开始键
你如何从列表中选择:~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
["dog", "cat", "sheep"] |> Enum.with_index(1) |> Enum.reduce(%{}, fn {item, index}, acc -> Map.put(acc, index, item) end)
returns
%{1 => "dog", 2 => "cat", 3 => "sheep"}
此处使用 Enum.with_index(1) 以索引 1 开始键