ken-kost

ken-kost OP

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:

First 2 of 2 Posts Switch mode

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

ken-kost

ken-kost OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement