shjeon0730

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

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

arcanemachine

Check out this post and read the rest of its thread as well:

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement