Weird behaviour of Enum.chunk_every

I met some weird behaviour of this function:

Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(2)> Enum.chunk_every([1,2,3,4,5],2)
[[1, 2], [3, 4], [5]]
iex(3)> Enum.chunk_every([1,2,3,4,5,6,7,8,],2)
[[1, 2], [3, 4], [5, 6], '\a\b']
iex(4)> Enum.chunk_every([1,2,3,4,5,6,7,8,9,10,11],2)
[[1, 2], [3, 4], [5, 6], '\a\b', '\t\n', '\v']
iex(5)> Enum.chunk_every([1,2,3,4,5,6,7,8,9,10,11,14],2)
[[1, 2], [3, 4], [5, 6], '\a\b', '\t\n', [11, 14]]
iex(6)> Enum.chunk_every([1,2,3,4,5,6,7,8,9,10,11,12,1314],2)
[[1, 2], [3, 4], [5, 6], '\a\b', '\t\n', '\v\f', [1314]]
iex(7)> Enum.chunk_every([1,2,3,4,5,6,7,8,9,10,11,12,13,14],2)
[[1, 2], [3, 4], [5, 6], '\a\b', '\t\n', '\v\f', [13, 14]]

Why does it do like that? My OS is Xubuntu 18.04.

Single quotes are character lists, how “strings” are handled in erlang. When your integers enter the printable ASCII range, elixir thinks they are character lists, converts the values to ASCII, and drops them inside the single quotes instead of the brackets.

If you are ever curious about something like '\t\n' but an i and a space in front of it in IEx, like:

iex(1)> i '\t\n'
Data type
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to
  as "charlists" (more precisely, a charlist is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [9, 10]
Reference modules
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars

Note the Raw Representation.

1 Like

So it’s only about how they are printed in the iex console?

Correct. ^.^

It’s just a helper to work with erlang strings better (which is what char-lists are, just lists of integers).

It’s the default presentation, if you do IO.inspect or Kernel.inspect it will be the same too.