Stream.chunk_while
You're seeing just the function
chunk_while
, go back to Stream module for more information.
Specs
chunk_while( Enumerable.t(), acc(), (element(), acc() -> {:cont, chunk, acc()} | {:cont, acc()} | {:halt, acc()}), (acc() -> {:cont, chunk, acc()} | {:cont, acc()}) ) :: Enumerable.t() when chunk: any()
Chunks the enum
with fine grained control when every chunk is emitted.
chunk_fun
receives the current element and the accumulator and
must return {:cont, element, acc}
to emit the given chunk and
continue with accumulator or {:cont, acc}
to not emit any chunk
and continue with the return accumulator.
after_fun
is invoked when iteration is done and must also return
{:cont, element, acc}
or {:cont, acc}
.
Examples
iex> chunk_fun = fn element, acc ->
...> if rem(element, 2) == 0 do
...> {:cont, Enum.reverse([element | acc]), []}
...> else
...> {:cont, [element | acc]}
...> end
...> end
iex> after_fun = fn
...> [] -> {:cont, []}
...> acc -> {:cont, Enum.reverse(acc), []}
...> end
iex> stream = Stream.chunk_while(1..10, [], chunk_fun, after_fun)
iex> Enum.to_list(stream)
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]