Ark.Ok (ark v0.10.1)

Copy Markdown View Source

Summary

Functions

Flat mapping while ok. Takes an enumerable and applies the given callback to all values of the enumerable as long as the callback returns {:ok, list}. The returned lists are flattened one level into the result.

Mapping while ok. Takes an enumerable and applies the given callback to all values of the enumerable as long as the callback returns {:ok, mapped_value}.

Wrapping ok.

Questionning ok.

Reducing while ok. Takes an enumerable, an initial value for the accumulator and a reducer function. Calls the reducer for each value in the enumerable as long as the reducer returns {:ok, new_acc}.

Unwrapping ok with raise.

Unwrapping ok, raising custom exceptions.

Functions

flat_map_ok(enum, f)

@spec flat_map_ok(Enumerable.t(), (term() -> {:ok, list()} | {:error, term()})) ::
  {:ok, list()} | {:error, term()}

Flat mapping while ok. Takes an enumerable and applies the given callback to all values of the enumerable as long as the callback returns {:ok, list}. The returned lists are flattened one level into the result.

Stops when the callback returns {:error, term} and returns that tuple.

Raises ArgumentError if the callback does not return a result tuple or if the ok tuple does not contain a list.

Returns {:ok, flat_mapped_values} or {:error, term}.

iex> flat_map_ok(1..3, fn v -> {:ok, [v, v * 10]} end)
{:ok, [1, 10, 2, 20, 3, 30]}

iex> flat_map_ok(1..3, fn 2 -> {:error, :three}; v -> {:ok, [v, v * 10]} end)
{:error, :three}

map_ok(enum, f)

@spec map_ok(Enumerable.t(), (term() -> {:ok, term()} | {:error, term()})) ::
  {:ok, list()} | {:error, term()}

Mapping while ok. Takes an enumerable and applies the given callback to all values of the enumerable as long as the callback returns {:ok, mapped_value}.

Stops when the callback returns {:error, term} and returns that tuple.

Raises ArgumentError if the callback does not return a result tuple.

Returns {:ok, mapped_values} or {:error, term}

iex> map_ok(1..4, fn v -> {:ok, v * v} end)
{:ok, [1, 4, 9, 16]}

iex> map_ok(1..4, fn 3 -> {:error, :three}; v -> {:ok, v * v} end)
{:error, :three}

ok(value)

Wrapping ok.

Converts a value to an :ok tuple, except when the value is:

  • the single atom :ok or an :ok tuple
  • the single atom :error or an :error tuple

ok?(value)

Questionning ok.

Returns true if the value is an {:ok, val} tuple or the single atom :ok.

Returns false otherwise.

reduce_ok(enum, initial, f)

@spec reduce_ok(Enumerable.t(), term(), (term(), term() ->
                                     {:ok, term()} | {:error, term()})) ::
  {:ok, term()} | {:error, term()}

Reducing while ok. Takes an enumerable, an initial value for the accumulator and a reducer function. Calls the reducer for each value in the enumerable as long as the reducer returns {:ok, new_acc}.

Stops when the reducer returns {:error, term} and returns that tuple.

Raises ArgumentError if the reducer does not return a result tuple.

iex> reduce_ok(1..4, 0, fn v, acc -> {:ok, acc + v} end)
{:ok, 10}

iex> reduce_ok(1..4, 0, fn 3, _acc -> {:error, :three}; v, acc -> {:ok, acc + v} end)
{:error, :three}

uok!(value)

Unwrapping ok with raise.

Unwraps an {:ok, val} tuple, giving only the value, or returns the single :ok atom as-is. Raises with any other value.

xok!(value)

Unwrapping ok, raising custom exceptions.

Much like uok!/1 but if an :error 2-tuple contains any exception as the second element, that exception will be raised.

Other values will lead to a generic Ark.Ok.UnwrapError exception to be raised.