`Enum.chunk_by`
I discovered a pretty cool Elixir function this weekend, Enum.chunk_by
.
Backstory: I wanted to group a list of strings by equality, while preserving the list order.
Enum.chunk_by
takes an enumerable and a function, and breaks that enumerable into an enumerable when the function returns a new or different result.
Here's a simple example:
Anytime l
changes, a new list is created.
Slightly more complex:
The function only returns something different (true
) on 3
, so 3
is assigned to its own list.