ken-kost

ken-kost

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:

Marked As Solved

lud

lud

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]]

Also Liked

ken-kost

ken-kost

: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:

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement