2

Chunking via Enum is behaving in a confusing manner. Here's the code I'm executing:

arr = [1,2,3,4,5,6,7]
out = Enum.chunk_every(arr, 3)

I'm expecting to get:

[[1, 2, 3], [4, 5, 6], [7]]

but what I'm actually getting is:

[[1, 2, 3], [4, 5, 6], '\a']

I don't understand what is happening to the [7] and even more confusing is the '\a' itself. I don't even know what that means in this context.

Additionally, if arr is [1,2,3,4,5,6,7,8] I get [[1, 2, 3], [4, 5, 6], '\a\b']

Here's another mind-blower. if I set arr to [1,2,3,4,5,6,7,8,9] (evenly divisible by 3) I get [[1, 2, 3], [4, 5, 6], '\a\b\t']

jaydel
  • 14,389
  • 14
  • 62
  • 98
  • Interesting. This leads to another question: is the interpretation of the last list as char_list just an artifact of IO.inspect or do I need to do some conversion on the element that is showing as a char_list into a list? – jaydel Apr 17 '19 at 15:42
  • 1
    Quoting from Justin's link: "When you use functions like inspect and IO.inspect, Elixir tries to be smart and format a list of integers as a string for easy readability. However, in some cases you end up with a nonsense string just because all of the integers in your list happen to be valid codepoints" – GavinBrelstaff Apr 17 '19 at 15:46
  • okay. I'll mark this as a duplicate. Thank you for a) pointing out that question, and b) answering my additional question :) – jaydel Apr 17 '19 at 15:49
  • Try: Enum.chunk_every(arr, 3, 3, :discard). You won't get the answer you "expected", but you will get a sensible list. You just won't get the [7] as your last element. – Liberty Lover Aug 30 '22 at 14:19

0 Answers0