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.