Here is an example of group_by / 3 in the Enum module.
Grouping an array of strings based on its length:
iex(12)> ["ant", "buffalo", "cat", "dingo"] |> Enum.group_by(&String.length/1)
%{3 => ["cat", "ant"], 5 => ["dingo"], 7 => ["buffalo"]}
From the docs:
Dividing a collection into groups based on pleasure.
The result is a dict (default map), where each key is a group, and each value is a list of items from the collection for which fun returned this group. The order is not necessarily saved.
source
share