View Source Bunch.List (Bunch v1.6.1)

A bunch of helper functions for list manipulation.

Summary

Functions

The version of unfoldr/2 that res_accepts :ok and :error return tuples.

Generates a list using generator function and provided initial accumulator.

Functions

@spec try_unfoldr(
  acc,
  (acc ->
     {:ok, {:cont, a, acc} | {:cont, acc} | :halt | {:halt, acc}}
     | {:error, reason})
) :: {:ok, [a] | {[a], acc}} | {:error, reason}
when acc: any(), a: any(), reason: any()

The version of unfoldr/2 that res_accepts :ok and :error return tuples.

Behaves as unfoldr/2 as long as :ok tuples are returned. Upon :error the processing is stopped and error is returned.

Examples

iex> f = fn
iex>   <<a, b, rest::binary>> ->
iex>     sum = a + b
iex>     if rem(sum, 2) == 1, do: {:ok, {:cont, sum, rest}}, else: {:error, :even_sum}
iex>   acc -> {:ok, {:halt, acc}}
iex> end
iex> Bunch.List.try_unfoldr(<<1,2,3,4,5>>, f)
{:ok, {[3, 7], <<5>>}}
iex> Bunch.List.try_unfoldr(<<2,4,6,8>>, f)
{:error, :even_sum}
@spec unfoldr(acc, (acc -> {:cont, a, acc} | {:cont, acc} | :halt | {:halt, acc})) ::
  [a] | {[a], acc}
when acc: any(), a: any()

Generates a list using generator function and provided initial accumulator.

Successive list elements are generated by calling f with the previous accumulator. The enumeration finishes when it returns :halt or {:halt, accumulator}.

Examples

iex> Bunch.List.unfoldr(10, fn 0 -> :halt; n -> {:cont, n-1} end)
Enum.to_list(9..0)
iex> f = fn
...>   <<size, content::binary-size(size), rest::binary>> -> {:cont, content, rest}
...>   binary -> {:halt, binary}
...> end
iex> Bunch.List.unfoldr(<<2, "ab", 3, "cde", 4, "fghi">>, f)
{~w(ab cde fghi), <<>>}
iex> Bunch.List.unfoldr(<<2, "ab", 3, "cde", 4, "fg">>, f)
{~w(ab cde), <<4, "fg">>}