加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

elixir Enum或任何其他模块是否具有类似于ruby的group_by的group

发布时间:2020-12-17 01:32:11 所属栏目:百科 来源:网络整理
导读:Ruby有这个令人敬畏的方法 group_by for Enumerable. Elixir有类似的东西吗?我在Enum模块上找不到此功能.谢谢 解决方法 以下是Enum模块中 group_by/3的示例. 根据字符串的长度对字符串数组进行分组: iex(12) ["ant","buffalo","cat","dingo"] | Enum.group
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读