Elixir: Enum.chunk_every turns some chunks into strings

Created on 2 Nov 2020  路  2Comments  路  Source: elixir-lang/elixir

Hello everyone,

Small issue I've recently picked up and wasn't able to debug the reason elsewhere.

Environment

Erlang/OTP 23 [erts-11.1.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]
Elixir 1.11.1 (compiled with Erlang/OTP 23)
Ubuntu 19.04

Current behavior

Non-working example:

iex> Enum.chunk_every([93, 65, 64, 42], 3, 3, :discard)
[']A@']
iex> Enum.chunk_every([53, 124, 118, 42], 3, 3, :discard)
['5|v']

Working example:

iex> Enum.chunk_every([42, 188, 75, 45], 3, 3, :discard)
[[42, 188, 75]]

Expected behavior

The chunking turns various arrays into strings where it should just give the numbers in the array.

The expected behaviour should be something like the following:

iex> Enum.chunk_every([93, 65, 64, 42], 3, 3, :discard)
[[93, 65, 64]]
iex> Enum.chunk_every([53, 124, 118, 42], 3, 3, :discard)
[[53, 124, 118]]

I'm very new to Elixir so there may be something I'm missing. I can't see why these specific cases may cause the strings so I thought I'd ask here.

Most helpful comment

Hi @colinschwegmann!

When Elixir seems a list of numbers where the numbers are represented as ascii characters, it prints them as a single quoted string, also known as "charlist". However, it is still a list at the end. Try this out on IEx:

iex(2)> i ']A@'
Term
  ']A@'
Data type
  List
Description
  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 printable
  ASCII characters. Conventionally, a list of Unicode code points is known as a
  charlist and a list of ASCII characters is a subset of it.
Raw representation
  [93, 65, 64]
Reference modules
  List
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars

See the Raw representation bit. :)

Have fun!

All 2 comments

Hi @colinschwegmann!

When Elixir seems a list of numbers where the numbers are represented as ascii characters, it prints them as a single quoted string, also known as "charlist". However, it is still a list at the end. Try this out on IEx:

iex(2)> i ']A@'
Term
  ']A@'
Data type
  List
Description
  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 printable
  ASCII characters. Conventionally, a list of Unicode code points is known as a
  charlist and a list of ASCII characters is a subset of it.
Raw representation
  [93, 65, 64]
Reference modules
  List
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars

See the Raw representation bit. :)

Have fun!

Thanks @josevalim!

Was this page helpful?
0 / 5 - 0 ratings