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.”

https://hexdocs.pm/elixir/main/binaries-strings-and-charlists.html

Also Liked

arcanemachine

arcanemachine

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

Where Next?

Popular in Questions Top

New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement