Ruby有这个令人敬畏的方法 group_by for Enumerable. Elixir有类似的东西吗?我在Enum模块上找不到此功能.谢谢 以下是Enum模块中 group_by/3的示例. 根据字符串的长度对字符串数组进行分组: iex
根据字符串的长度对字符串数组进行分组:
iex(12)> ["ant", "buffalo", "cat", "dingo"] |> Enum.group_by(&String.length/1) %{3 => ["cat", "ant"], 5 => ["dingo"], 7 => ["buffalo"]}
来自文档:
Splits collection into groups based on a fun.
The result is a dict (by default a map) where each key is a group and each value is a list of elements from collection for which fun returned that group. Ordering is not necessarily preserved.