[newbie here] Strange caracter while using Enum.chunk_every command in iex

Hi people!
I’ve just started learning Elixir this week.

I executed this command in iex and the console output was really strange, could anyone tell me why?

Command:

Enum.chunk_every([1,2,3,4,5,6,7,8,9,10,11,12,13,14], 2)

Return:

[[1, 2], [3, 4], [5, 6], ~c"\a\b", ~c"\t\n", ~c"\v\f", [13, 14]]

What happened to the numbers 7,8,9,10,11 and 12?

Thank you all and have a great day!

Hey @renanmuniz,

What’s happening is that [7,8], [9,10], [11, 12] are being interpreted as Charlists.

A charlist is a list of integers where all the integers are valid code points.

To see it as non-charlists, you can try piping it the result of your Enum.chunk_every/2 into an inspect/2 function.

inspect(result, charlists: :as_list)
4 Likes

Thanks @shawn_leong !
But why this happens only with this numbers(7~12) and not with all of them?

Because only those are printable (well, in this case they are escape characters for whitespaces but you get the idea).

1 Like

So every time we need to check some return value, logging messages for example, we need to use the:

inspect(result, charlists: :as_list)

?

If you’re interested in the underlying integer values, yes. The problem is that erlang (being the system underlying elixir) does use charlists for text, which you generally also want to be able to read (and not see a list of numbers). Therefore elixir uses a heuristic to figure out which one you might be interested in. That heuristic is imperfect, but better than not having it.

You can find that discussed more elaborately in the docs: List — Elixir v1.16.1

4 Likes

Yes, kind of. Though in real production systems you want logging and telemetry traces / spans.

As for development environment, you can just create the file .iex.exs in your project’s root directory and put this there:

IEx.configure(inspect: [charlists: :as_list])

And you’ll have that behaviour for your each local iex session.

2 Likes

Got it, thank you @LostKobrakai!

Nice! Thanks @dimitarvp!
Think I’m done now!

*inspect: [charlists: :as_list]

3 Likes

Thanks, corrected.

2 Likes