shjeon0730
Does Enum.chunk_by have a bug?
Hi!
I am new in Elixir. Still learning syntax. But I I see very weird behavior while I am testing functions.
The question is about Enum.chunk_by function.
list=[1,2,3,4,5,6,7,8,9,10]
Enum.chunk_by(list, & rem(&1,4)==0)
expected result is,
[[1,2,3],[4],[5,6,7],[8],[9,10]]
but, what I am receiving is
[[1, 2, 3], [4], [5, 6, 7], ~c"\b", ~c"\t\n"]
My elixir version is 1.17.3, on MacOS 15.1.1, M1 Pro
I am curious if there is some system-relative background of this result.
Thank you!
===
Answer for this question:
turned out ~c"\b" is equivalent to [8], and ~c"\t\n" is equivalent to [9,10]
as the chosen answer explains. This looks like a common mistake that a beginner is confused (like me). Thanks for all the answers! I couldn’t understand the short answers, but after I understand what is happening, even the short answers makes sense.
Marked As Solved
llama
Short answer:
“This is done to ease interoperability with Erlang, even though it may lead to some surprising behavior. For example, if you are storing a list of integers that happen to range between 0 and 127, by default IEx will interpret this as a charlist and it will display the corresponding ASCII characters.”
Also Liked
arcanemachine
Popular in Questions
Other popular topics
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









