# `Ark.Ok`
[🔗](https://github.com/lud/ark/blob/main/lib/ark/ok.ex#L1)

# `flat_map_ok`

```elixir
@spec flat_map_ok(Enumerable.t(), (term() -&gt; {: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`

```elixir
@spec map_ok(Enumerable.t(), (term() -&gt; {: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`

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?`

Questionning ok.

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

Returns `false` otherwise.

# `reduce_ok`

```elixir
@spec reduce_ok(Enumerable.t(), term(), (term(), term() -&gt;
                                     {: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!`

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!`

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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
