Enum.reduce_while

You're seeing just the function reduce_while, go back to Enum module for more information.
Link to this function

reduce_while(enumerable, acc, fun)

View Source

Specs

reduce_while(t(), any(), (element(), any() -> {:cont, any()} | {:halt, any()})) ::
  any()

Reduces enumerable until fun returns {:halt, term}.

The return value for fun is expected to be

  • {:cont, acc} to continue the reduction with acc as the new accumulator or
  • {:halt, acc} to halt the reduction

If fun returns {:halt, acc} the reduction is halted and the function returns acc. Otherwise, if the enumerable is exhausted, the function returns the accumulator of the last {:cont, acc}.

Examples

iex> Enum.reduce_while(1..100, 0, fn x, acc ->
...>   if x < 5, do: {:cont, acc + x}, else: {:halt, acc}
...> end)
10
iex> Enum.reduce_while(1..100, 0, fn x, acc ->
...>   if x > 0, do: {:cont, acc + x}, else: {:halt, acc}
...> end)
5050