Difference between group_by and chunk_by

So I was solving an advent of code challenge and stumbled upon a behavior that was unexpected to me so I’m making a query here to find out what might be the reason.
Here is an example:

Erlang/OTP 27 [erts-15.1.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.17.3) - press Ctrl+C to exit (type h() ENTER for help)
# Example 1
iex(1)> list = [{1,3}, {1,4}, {2,3}, {2,4}]
[{1, 3}, {1, 4}, {2, 3}, {2, 4}]
iex(2)> Enum.group_by list, fn {x, _y} -> x end
%{1 => [{1, 3}, {1, 4}], 2 => [{2, 3}, {2, 4}]}
iex(3)> Enum.chunk_by list, fn {x, _y} -> x end
[[{1, 3}, {1, 4}], [{2, 3}, {2, 4}]]
iex(4)> Enum.group_by list, fn {_x, y} -> y end
%{3 => [{1, 3}, {2, 3}], 4 => [{1, 4}, {2, 4}]}
iex(5)> Enum.chunk_by list, fn {_x, y} -> y end
[[{1, 3}], [{1, 4}], [{2, 3}], [{2, 4}]]

# Example 2
iex(6)> list = [{{1, 3}, []}, {{1, 4}, []}, {{2, 3}, []}, {{2, 4}, []}]
[{{1, 3}, []}, {{1, 4}, []}, {{2, 3}, []}, {{2, 4}, []}]
iex(7)> Enum.group_by list, fn {{x, _y}, _} -> x end
%{1 => [{{1, 3}, []}, {{1, 4}, []}], 2 => [{{2, 3}, []}, {{2, 4}, []}]}
iex(8)> Enum.chunk_by list, fn {{x, _y}, _} -> x end
[[{{1, 3}, []}, {{1, 4}, []}], [{{2, 3}, []}, {{2, 4}, []}]]
iex(9)> Enum.group_by list, fn {{_x, y}, _} -> y end
%{3 => [{{1, 3}, []}, {{2, 3}, []}], 4 => [{{1, 4}, []}, {{2, 4}, []}]}
iex(10)> Enum.chunk_by list, fn {{_x, y}, _} -> y end
[[{{1, 3}, []}], [{{1, 4}, []}], [{{2, 3}, []}], [{{2, 4}, []}]]
iex(11)> 

First example works as expected. I have a list of points and I group/chunk them by x and by y.
For the second example difference is that the point is wrapped into a tuple with some additional element (empty list). so {x,y} => {{x,y}, []}
group_by and chunk_by behave the same for x, but not for y in the second example. Why is that? I assume it’s not a bug but I wasn’t expecting this, I’m curious what could be the reason. :bug:

Your X are ordered.

In example 1, chunk_by sees x=1, x=1 then x=2, so it emits a chunk for both 1, then starts a new chunk and finally x=2 makes it to the same chunk.

In the second example, you have, 3, 4, 3, 4, so that is 4 chunks because at each number, a new chunk is started.

iex(1)> Enum.chunk_by([1,1,2,2], & &1)
[[1, 1], [2, 2]]
iex(2)> Enum.chunk_by([1,2,1,2], & &1)
[[1], [2], [1], [2]]

1 Like

:man_facepalming: Yea, I didn’t thought it through, group by does not behave the same as chunk by, duh. Thanks for the explanation. :star2:

1 Like