ken-kost
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. ![]()
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 2 of 2 Posts
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.
ken-kost