Hi.
I was taking a look into the Enum info page... and I think that I had found a trouble. The chunk_by function doesn麓t return the same result on my iex console that on the example:
Theese are my results:
iex(15)> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6], '\a\a']
iex(16)> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6]]
iex(17)> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7, 6], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6], '\a\a', [6]]
iex(18)> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 8, 7, 6], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6], '\a', '\b', '\a', [6]]
iex(19)> Enum.chunk_by([1, 7, 2, 2, 3, 4, 4, 6, 7, 8, 7, 6], &(rem(&1, 2) == 1))
[[1, 7], [2, 2], [3], [4, 4, 6], '\a', '\b', '\a', [6]]
iex(20)> Enum.chunk_by([1, 7, 2, 2, 3, 4, 7, 4, 6, 7, 8, 7, 6], &(rem(&1, 2) == 1))
[[1, 7], [2, 2], [3], [4], '\a', [4, 6], '\a', '\b', '\a', [6]]
iex(21)> Enum.chunk_by([1, 7, 2, 2, 3, 4, 7, 4, 6, 3, 8, 7, 6], &(rem(&1, 2) == 1))
[[1, 7], [2, 2], [3], [4], '\a', [4, 6], [3], '\b', '\a', [6]]
iex(22)> Enum.chunk_by([1, 7, 2, 2, 3, 4, 7, 4, 6, 3, 8, 2, 7, 6], &(rem(&1, 2) == 1))
[[1, 7], [2, 2], [3], [4], '\a', [4, 6], [3], [8, 2], '\a', [6]]
The result of the example is this:
iex> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
My first attempt iex(15) is exactly the same that the example and the results don麓t match. My quick conclusion is that thes funcition needs, at least, two consecutive even numbers to work properly.
And... about the example... shouldn麓t be this one [[1], [2, 2], [3], [4, 4, 6], [7], [7]] the correct solution??
The version I am using (for windows):
Eshell V8.3 (abort with ^G)
IEx 1.4.4
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6], '\a\a']
elixir 1.5.1 gives same result but I think Enum.chunk_by works correctly and this is related to char list representation. could you try following code?
iex(2)> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1)) == [[1], [2, 2], [3], [4, 4, 6], [7, 7]]
true
@eloyucu Your IEx results show Enum.chunk_by works correctly. The function works by chunking _only_ when the callback鈥檚 result _changes_.
For this example,
Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
Iterating through ...
rem(1, 2) == 1 is true. Result is [[1]].
rem(2, 2) == 1 is false (changed). Result is [[1], [2]].
rem(2, 2) == 1 is false (same). Result is [[1], [2, 2]].
rem(3, 2) == 1 is true (changed). Result is [[1], [2, 2], [3]].
... until ...
rem(7, 2) == 1 is true (changed). Result is [[1], [2, 2], [3], [4, 4, 6], [7]].
rem(7, 2) == 1 is true (same). Result is [[1], [2, 2], [3], [4, 4, 6], [7, 7]].
So [[1], [2, 2], [3], [4, 4, 6], [7, 7]] is the correct result.
When all numbers in a list is a printable character, Elixir shows it using the single quoted syntax. Type i '\a\a' in IEx for more information!
Thanks guys.
You are right... the representation is the key!! :)
And sorry... but I saw this difference between my results and the example on the official web and I thought it was a mistake.
Most helpful comment
When all numbers in a list is a printable character, Elixir shows it using the single quoted syntax. Type
i '\a\a'in IEx for more information!