Hi,
I have a problem with Stream.chunk_while
in combination with Enum.at
(elixir version is 1.5.1)
here is the code:
chunk_fun = fn
i, [] ->
{:cont, [i]}
i, chunk ->
if rem(i, 2) == 0 do
{:cont, Enum.reverse(chunk), [i]}
else
{:cont, [i | chunk]}
end
end
after_fun = fn
[] -> {:cont, []}
chunk -> {:cont, Enum.reverse(chunk), []}
end
stream = [1, 2, 3, 4, 5] |> Stream.chunk_while([], chunk_fun, after_fun)
calling stream |> Enum.at(0)
gives me this error:
** (ArithmeticError) bad argument in arithmetic expression
(elixir) lib/enum.ex:793: anonymous fn/3 in Enum.fetch_enumerable/3
(elixir) lib/stream.ex:1413: anonymous fn/3 in Enumerable.Stream.reduce/3
(elixir) lib/stream.ex:234: Stream.after_chunk_while/3
(elixir) lib/stream.ex:1446: Enumerable.Stream.do_done/2
(elixir) lib/enum.ex:789: Enum.fetch_enumerable/3
(elixir) lib/enum.ex:311: Enum.at/3
but it’s ok to run stream with Enum.to_list
:
> Enum.to_list(stream)
[[1], [2, 3], [4, 5]]
also it’s ok to get last chunk like so:
> stream |> Enum.at(2)
[4, 5]
Probably I’m doing something wrong?