Elixir的Enum模块里有没有类似Ruby group_by功能的函数?
- 内容介绍
- 文章标签
- 相关推荐
本文共计205个文字,预计阅读时间需要1分钟。
Ruby 中有这个令人尊敬的方法 +group_by+ for Enumerable. Elixir 有类似的东西吗?我在 Enum 模块上找不到这个功能。谢谢!以下是在 Enum 模块中 group_by/3 的示例。根据字符串的长度对字符串数组进行分组:
Ruby有这个令人敬畏的方法 group_by for Enumerable. Elixir有类似的东西吗?我在Enum模块上找不到此功能.谢谢 以下是Enum模块中 group_by/3的示例.根据字符串的长度对字符串数组进行分组:
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.
本文共计205个文字,预计阅读时间需要1分钟。
Ruby 中有这个令人尊敬的方法 +group_by+ for Enumerable. Elixir 有类似的东西吗?我在 Enum 模块上找不到这个功能。谢谢!以下是在 Enum 模块中 group_by/3 的示例。根据字符串的长度对字符串数组进行分组:
Ruby有这个令人敬畏的方法 group_by for Enumerable. Elixir有类似的东西吗?我在Enum模块上找不到此功能.谢谢 以下是Enum模块中 group_by/3的示例.根据字符串的长度对字符串数组进行分组:
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.

