

OnePiece.Result — OnePiece.Result v0.4.0

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L1 "View Source")

OnePiece.Result

(OnePiece.Result v0.4.0)

================================================================================================================================================================================================================

Handles [`t/0`](#t:t/0) responses. Inspired by Rust `std::result::Result` package.

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[err()](#t:err/0)

An Error result.

[ok()](#t:ok/0)

An Ok result.

[t()](#t:t/0)

An Result Tuple.

[Functions](#functions)

------------------------

[and\_err(first\_result, second\_result)](#and_err/2)

Do an `and` with a [`err/0`](#t:err/0).

[and\_ok(first\_result, second\_result)](#and_ok/2)

Do an `and` with a [`ok/0`](#t:ok/0).

[collect(result\_list)](#collect/1)

Iterate over [`t/0`](#t:t/0) list unwrapping the [`ok/0`](#t:ok/0) values. It fails at the first [`err/0`](#t:err/0).

[contains\_err?(arg, value)](#contains_err?/2)

Returns true if the [`err/0`](#t:err/0) result contains the given value.

[contains\_ok?(arg, value)](#contains_ok?/2)

Returns true if the [`ok/0`](#t:ok/0) result contains the given value.

[err(reason)](#err/1)

Wraps a value into an [`err/0`](#t:err/0) result.

[expect\_err!(arg, message)](#expect_err!/2)

Returns the contained [`ok/0`](#t:ok/0) value, or raise an exception with the given error message.

[expect\_ok!(arg, message)](#expect_ok!/2)

Returns the contained [`ok/0`](#t:ok/0) value, or raise an exception with the given error message.

[flatten(value)](#flatten/1)

Converts from a nested [`t/0`](#t:t/0) to flatten [`t/0`](#t:t/0).

[is\_err?(arg)](#is_err?/1)

Returns true if the argument is an [`err/0`](#t:err/0).

[is\_err\_and?(arg, func)](#is_err_and?/2)

Returns true if the result is [`err/0`](#t:err/0) and the value inside of it matches a predicate.

[is\_err\_result(val)](#is_err_result/1)

Is valid if and only if an [`err/0`](#t:err/0) is supplied.

[is\_ok?(arg)](#is_ok?/1)

Returns true if the argument is a [`ok/0`](#t:ok/0).

[is\_ok\_and?(arg, func)](#is_ok_and?/2)

Returns true if the result is [`ok/0`](#t:ok/0) and the value inside of it matches a predicate.

[is\_ok\_result(val)](#is_ok_result/1)

Is valid if and only if an [`ok/0`](#t:ok/0) is supplied.

[map\_err(result, on\_error)](#map_err/2)

When the value contained in an [`err/0`](#t:err/0) result then applies a function or returns the mapped value, wrapping the

returning value in a [`err/0`](#t:err/0), propagating the [`ok/0`](#t:ok/0) result as it is.

[map\_ok(error, on\_ok)](#map_ok/2)

When the value contained in an [`ok/0`](#t:ok/0) result then applies a function or returns the mapped value, wrapping the

returning value in a [`ok/0`](#t:ok/0), propagating the [`err/0`](#t:err/0) result as it is.

[map\_ok\_or(arg, on\_ok, on\_error)](#map_ok_or/3)

Applies a `on_ok` function to the contained value if [`ok/0`](#t:ok/0) otherwise applies a `on_err` function or return the

`on_err` value if [`err/0`](#t:err/0).

[ok(value)](#ok/1)

Wraps a value into an [`ok/0`](#t:ok/0) result.

[reject\_nil(response, on\_nil)](#reject_nil/2)

When `nil` is passed then calls the `on_nil` function and wrap the result into a [`err/0`](#t:err/0). When [`t/0`](#t:t/0) is passed

then returns it as it is. Otherwise, wraps the value into a [`ok/0`](#t:ok/0).

[tap\_err(result, func)](#tap_err/2)

Tap into [`err/0`](#t:err/0) results.

[tap\_ok(result, func)](#tap_ok/2)

Tap into [`ok/0`](#t:ok/0) results.

[unwrap(arg)](#unwrap/1)

Returns the contained [`ok/0`](#t:ok/0) value or `t:error/0` value from a [`t/0`](#t:t/0).

[unwrap\_err!(arg)](#unwrap_err!/1)

Unwrap an [`err/0`](#t:err/0) result, or raise an exception.

[unwrap\_ok(arg, on\_error)](#unwrap_ok/2)

Returns the contained [`ok/0`](#t:ok/0) value or a provided default.

[unwrap\_ok!(arg)](#unwrap_ok!/1)

Unwrap an [`ok/0`](#t:ok/0) result, or raise an exception.

[when\_err(resp, on\_err)](#when_err/2)

Applies a function or returns the value if the result is [`err/0`](#t:err/0), otherwise returns the [`ok/0`](#t:ok/0) value.

[when\_ok(error, on\_ok)](#when_ok/2)

Applies a function or returns the value when a [`ok/0`](#t:ok/0) is given, or propagates the error.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:err/0 "Link to this type")

err()

=====

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L18 "View Source")

```

@type err() :: {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

An Error result.

[Link to this type](#t:ok/0 "Link to this type")

ok()

====

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L13 "View Source")

```

@type ok() :: {:ok, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

An Ok result.

[Link to this type](#t:t/0 "Link to this type")

t()

===

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L23 "View Source")

```

@type t() :: [ok](#t:ok/0)() | [err](#t:err/0)()

```

An Result Tuple.

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#and_err/2 "Link to this function")

and\_err(first\_result, second\_result)

=======================================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L513 "View Source")

```

@spec and_err(first_result :: [t](#t:t/0)(), second_result :: [t](#t:t/0)()) :: [t](#t:t/0)()

```

Do an `and` with a [`err/0`](#t:err/0).

When passing any [`ok/0`](#t:ok/0) result, it returns the first [`ok/0`](#t:ok/0) value. Otherwise, when passing two [`err/0`](#t:err/0) results,

then returns the second [`err/0`](#t:err/0).

```

iex> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.and\_err(OnePiece.Result.ok(42))

{:ok, 21}

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.and\_err(OnePiece.Result.err("something went wrong"))

{:ok, 42}

iex> "something went wrong"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.and\_err(OnePiece.Result.ok(42))

{:ok, 42}

iex> "something went wrong"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.and\_err(OnePiece.Result.err("late error"))

{:error, "late error"}

```

[Link to this function](#and_ok/2 "Link to this function")

and\_ok(first\_result, second\_result)

======================================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L330 "View Source")

```

@spec and_ok(first_result :: [t](#t:t/0)(), second_result :: [t](#t:t/0)()) :: [t](#t:t/0)()

```

Do an `and` with a [`ok/0`](#t:ok/0).

When passing two [`ok/0`](#t:ok/0) result, it returns the second [`ok/0`](#t:ok/0) value. When a [`ok/0`](#t:ok/0) and a [`err/0`](#t:err/0) result, then

returns the [`err/0`](#t:err/0). Otherwise, when passing two [`err/0`](#t:err/0) result, it returns the earliest [`err/0`](#t:err/0).

```

iex> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.and\_ok(OnePiece.Result.ok(42))

{:ok, 42}

iex> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.and\_ok(OnePiece.Result.err("something went wrong"))

{:error, "something went wrong"}

iex> "something went wrong"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.and\_ok(OnePiece.Result.ok(42))

{:error, "something went wrong"}

iex> "something went wrong"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.and\_ok(OnePiece.Result.err("late error"))

{:error, "something went wrong"}

```

[Link to this function](#collect/1 "Link to this function")

collect(result\_list)

=====================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L662 "View Source")

```

@spec collect([Enum.t](https://hexdocs.pm/elixir/Enum.html#t:t/0)()) :: [t](#t:t/0)()

```

Iterate over [`t/0`](#t:t/0) list unwrapping the [`ok/0`](#t:ok/0) values. It fails at the first [`err/0`](#t:err/0).

```

iex> OnePiece.Result.collect([

...>  OnePiece.Result.ok(21),

...>  OnePiece.Result.ok(42),

...>  OnePiece.Result.ok(84),

...> ])

{:ok, [21,42,84]}

iex> OnePiece.Result.collect([

...>  OnePiece.Result.ok(21),

...>  OnePiece.Result.err("oops"),

...>  OnePiece.Result.ok(84),

...> ])

{:error, "oops"}

```

[Link to this function](#contains_err?/2 "Link to this function")

contains\_err?(arg, value)

==========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L205 "View Source")

```

@spec contains_err?(result :: [t](#t:t/0)(), value :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns true if the [`err/0`](#t:err/0) result contains the given value.

```

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.contains\_err?("oops")

true

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.contains\_err?("nop")

false

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.contains\_err?("ops")

false

```

[Link to this function](#contains_ok?/2 "Link to this function")

contains\_ok?(arg, value)

=========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L182 "View Source")

```

@spec contains_ok?(result :: [t](#t:t/0)(), value :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns true if the [`ok/0`](#t:ok/0) result contains the given value.

```

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.contains\_ok?(42)

true

iex> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.contains\_ok?(42)

false

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.contains\_ok?(42)

false

```

[Link to this function](#err/1 "Link to this function")

err(reason)

===========

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L41 "View Source")

```

@spec err(reason :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [err](#t:err/0)()

```

Wraps a value into an [`err/0`](#t:err/0) result.

```

iex> OnePiece.Result.err("oops")

{:error, "oops"}

```

[Link to this function](#expect_err!/2 "Link to this function")

expect\_err!(arg, message)

==========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L565 "View Source")

```

@spec expect_err!(result :: [t](#t:t/0)(), message :: [String.t](https://hexdocs.pm/elixir/String.html#t:t/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | [no\_return](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns the contained [`ok/0`](#t:ok/0) value, or raise an exception with the given error message.

```

iex> try do

...>  42

...>  |> OnePiece.Result.ok()

...>  |> OnePiece.Result.expect\_err!("expected a oops")

...> rescue

...>  e -> e.message

...> end

"expected a oops"

iex> try do

...>  "oops"

...>  |> OnePiece.Result.err()

...>  |> OnePiece.Result.expect\_err!("expected a oops")

...> rescue

...>  \_ -> "was a unwrap failure"

...> end

"oops"

```

[Link to this function](#expect_ok!/2 "Link to this function")

expect\_ok!(arg, message)

=========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L415 "View Source")

```

@spec expect_ok!(result :: [t](#t:t/0)(), message :: [String.t](https://hexdocs.pm/elixir/String.html#t:t/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | [no\_return](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns the contained [`ok/0`](#t:ok/0) value, or raise an exception with the given error message.

```

iex> try do

...>  21

...>  |> OnePiece.Result.err()

...>  |> OnePiece.Result.expect\_ok!("expected 42")

...> rescue

...>  e -> e.message

...> end

"expected 42"

iex> try do

...>  42

...>  |> OnePiece.Result.ok()

...>  |> OnePiece.Result.expect\_ok!("expected 42")

...> rescue

...>  \_ -> "was a unwrap failure"

...> end

42

```

[Link to this function](#flatten/1 "Link to this function")

flatten(value)

==============

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L642 "View Source")

```

@spec flatten(result :: [t](#t:t/0)()) :: [t](#t:t/0)()

```

Converts from a nested [`t/0`](#t:t/0) to flatten [`t/0`](#t:t/0).

```

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.flatten()

{:ok, 42}

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.flatten()

{:error, {:ok, 42}}

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.flatten()

{:error, "oops"}

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.flatten()

{:error, {:error, "oops"}}

```

[Link to this function](#is_err?/1 "Link to this function")

is\_err?(arg)

=============

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L93 "View Source")

```

@spec is_err?(value :: [t](#t:t/0)()) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns true if the argument is an [`err/0`](#t:err/0).

```

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.is\_err?()

false

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.is\_err?()

true

```

[Link to this function](#is_err_and?/2 "Link to this function")

is\_err\_and?(arg, func)

========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L112 "View Source")

```

@spec is_err_and?(value :: [t](#t:t/0)(), predicate :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)())) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns true if the result is [`err/0`](#t:err/0) and the value inside of it matches a predicate.

```

iex> is\_not\_found = fn err -> err == :not\_found end

...> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.is\_err\_and?(is\_not\_found)

false

iex> is\_not\_found = fn err -> err == :not\_found end

...> :not\_found

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.is\_err\_and?(is\_not\_found)

true

```

[Link to this macro](#is_err_result/1 "Link to this macro")

is\_err\_result(val)

====================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L161 "View Source")

(macro)

```

@spec is_err_result(value :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [Macro.t](https://hexdocs.pm/elixir/Macro.html#t:t/0)()

```

Is valid if and only if an [`err/0`](#t:err/0) is supplied.

```

iex> check = fn

...>  val when OnePiece.Result.is\_err\_result(val) -> true

...>  \_ -> false

...> end

...> 42

...> |> OnePiece.Result.ok()

...> |> check.()

false

iex> check = fn

...>  val when OnePiece.Result.is\_err\_result(val) -> true

...>  \_ -> false

...> end

...> "oops"

...> |> OnePiece.Result.err()

...> |> check.()

true

```

[Link to this function](#is_ok?/1 "Link to this function")

is\_ok?(arg)

============

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L57 "View Source")

```

@spec is_ok?(value :: [t](#t:t/0)()) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns true if the argument is a [`ok/0`](#t:ok/0).

```

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.is\_ok?()

true

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.is\_ok?()

false

```

[Link to this function](#is_ok_and?/2 "Link to this function")

is\_ok\_and?(arg, func)

=======================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L76 "View Source")

```

@spec is_ok_and?(value :: [t](#t:t/0)(), predicate :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)())) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Returns true if the result is [`ok/0`](#t:ok/0) and the value inside of it matches a predicate.

```

iex> is\_meaning\_of\_life = fn x -> x == 42 end

...> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.is\_ok\_and?(is\_meaning\_of\_life)

true

iex> is\_meaning\_of\_life = fn x -> x == 42 end

...> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.is\_ok\_and?(is\_meaning\_of\_life)

false

```

[Link to this macro](#is_ok_result/1 "Link to this macro")

is\_ok\_result(val)

===================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L137 "View Source")

(macro)

```

@spec is_ok_result(value :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [Macro.t](https://hexdocs.pm/elixir/Macro.html#t:t/0)()

```

Is valid if and only if an [`ok/0`](#t:ok/0) is supplied.

```

iex> check = fn

...>  val when OnePiece.Result.is\_ok\_result(val) -> true

...>  \_ -> false

...> end

...> 42

...> |> OnePiece.Result.ok()

...> |> check.()

true

iex> check = fn

...>  val when OnePiece.Result.is\_ok\_result(val) -> true

...>  \_ -> false

...> end

...> "oops"

...> |> OnePiece.Result.err()

...> |> check.()

false

```

[Link to this function](#map_err/2 "Link to this function")

map\_err(result, on\_error)

===========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L456 "View Source")

```

@spec map_err(result :: [t](#t:t/0)(), on_error :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) | [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [t](#t:t/0)()

```

When the value contained in an [`err/0`](#t:err/0) result then applies a function or returns the mapped value, wrapping the

returning value in a [`err/0`](#t:err/0), propagating the [`ok/0`](#t:ok/0) result as it is.

> #### Avoid Wrapping

> 

> If you want to avoid the wrap then use [`OnePiece.Result.when_err/2`](#when_err/2) instead.

> 

> 

```

 iex> 21

 ...> |> OnePiece.Result.err()

 ...> |> OnePiece.Result.map\_err("must be 42")

 {:error, "must be 42"}

 iex> 42

 ...> |> OnePiece.Result.ok()

 ...> |> OnePiece.Result.map\_err("must be 42")

 {:ok, 42}

```

You can also pass a function to achieve lazy evaluation:

```

iex> meaning\_of\_life = fn x -> "must be 42 instead of #{x}" end

...> 21

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.map\_err(meaning\_of\_life)

{:error, "must be 42 instead of 21"}

```

[Link to this function](#map_ok/2 "Link to this function")

map\_ok(error, on\_ok)

======================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L235 "View Source")

```

@spec map_ok(result :: [t](#t:t/0)(), on_ok :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) | [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [t](#t:t/0)()

```

When the value contained in an [`ok/0`](#t:ok/0) result then applies a function or returns the mapped value, wrapping the

returning value in a [`ok/0`](#t:ok/0), propagating the [`err/0`](#t:err/0) result as it is.

> #### Avoid Wrapping

> 

> If you want to avoid the wrap then use [`OnePiece.Result.when_ok/2`](#when_ok/2) instead.

> 

> 

```

iex> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.map\_ok(42)

{:ok, 42}

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.map\_ok(42)

{:error, "oops"}

```

You can also pass a function to achieve lazy evaluation:

```

iex> meaning\_of\_life = fn x -> x \* 2 end

...> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.map\_ok(meaning\_of\_life)

{:ok, 42}

```

[Link to this function](#map_ok_or/3 "Link to this function")

map\_ok\_or(arg, on\_ok, on\_error)

===================================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L273 "View Source")

```

@spec map_ok_or(

  result :: [t](#t:t/0)(),

  on_ok :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()),

  on_error :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Applies a `on_ok` function to the contained value if [`ok/0`](#t:ok/0) otherwise applies a `on_err` function or return the

`on_err` value if [`err/0`](#t:err/0).

```

iex> meaning\_of\_life = fn x -> x \* 2 end

...> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.map\_ok\_or(meaning\_of\_life, 84)

42

iex> meaning\_of\_life = fn x -> x \* 2 end

...> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.map\_ok\_or(meaning\_of\_life, 84)

84

```

> #### Lazy Evaluation

> 

> It is recommended to pass a function as the default value, which is lazily evaluated.

> 

> 

```

iex> meaning\_of\_life = fn x -> x \* 2 end

...> went\_wrong = fn reason -> "something went wrong because #{reason}" end

...> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.map\_ok\_or(meaning\_of\_life, went\_wrong)

42

iex> meaning\_of\_life = fn x -> x \* 2 end

...> went\_wrong = fn reason -> "something went wrong because #{reason}" end

...> "a sleepy bear"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.map\_ok\_or(meaning\_of\_life, went\_wrong)

"something went wrong because a sleepy bear"

```

[Link to this function](#ok/1 "Link to this function")

ok(value)

=========

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L32 "View Source")

```

@spec ok(value :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [ok](#t:ok/0)()

```

Wraps a value into an [`ok/0`](#t:ok/0) result.

```

iex> OnePiece.Result.ok(42)

{:ok, 42}

```

[Link to this function](#reject_nil/2 "Link to this function")

reject\_nil(response, on\_nil)

==============================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L608 "View Source")

```

@spec reject_nil(value :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), on_nil :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | (() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())) :: [t](#t:t/0)()

```

When `nil` is passed then calls the `on_nil` function and wrap the result into a [`err/0`](#t:err/0). When [`t/0`](#t:t/0) is passed

then returns it as it is. Otherwise, wraps the value into a [`ok/0`](#t:ok/0).

```

iex> OnePiece.Result.reject\_nil(nil, "ooopps")

{:error, "ooopps"}

iex> OnePiece.Result.reject\_nil(42, "ooopps")

{:ok, 42}

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.reject\_nil("ooopps")

{:ok, 42}

iex> "my error"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.reject\_nil("ooopps")

{:error, "my error"}

```

> #### Lazy Evaluation

> 

> It is recommended to pass a function as the default value, which is lazily evaluated.

> 

> 

```

iex> new\_error = fn -> "ooops" end

...> OnePiece.Result.reject\_nil(nil, new\_error)

{:error, "ooops"}

```

[Link to this function](#tap_err/2 "Link to this function")

tap\_err(result, func)

======================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L578 "View Source")

```

@spec tap_err(result :: [t](#t:t/0)(), func :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())) :: [t](#t:t/0)()

```

Tap into [`err/0`](#t:err/0) results.

```

iex> failure\_log = fn err -> "Failure because #{err}" end

...> "ooopsy"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.tap\_err(failure\_log)

{:error, "ooopsy"}

```

[Link to this function](#tap_ok/2 "Link to this function")

tap\_ok(result, func)

=====================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L428 "View Source")

```

@spec tap_ok(result :: [t](#t:t/0)(), func :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())) :: [t](#t:t/0)()

```

Tap into [`ok/0`](#t:ok/0) results.

```

iex> success\_log = fn x -> "Success #{x}" end

...> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.tap\_ok(success\_log)

{:ok, 42}

```

[Link to this function](#unwrap/1 "Link to this function")

unwrap(arg)

===========

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L685 "View Source")

```

@spec unwrap(result :: [t](#t:t/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Returns the contained [`ok/0`](#t:ok/0) value or `t:error/0` value from a [`t/0`](#t:t/0).

```

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.unwrap()

42

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.unwrap()

"oops"

```

[Link to this function](#unwrap_err!/1 "Link to this function")

unwrap\_err!(arg)

=================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L540 "View Source")

```

@spec unwrap_err!(result :: [t](#t:t/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | [no\_return](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Unwrap an [`err/0`](#t:err/0) result, or raise an exception.

```

iex> try do

...>  "oops"

...>  |> OnePiece.Result.err()

...>  |> OnePiece.Result.unwrap\_err!()

...> rescue

...>  OnePiece.Result.ErrUnwrapError -> "was a unwrap failure"

...> end

"oops"

iex> try do

...>  42

...>  |> OnePiece.Result.ok()

...>  |> OnePiece.Result.unwrap\_err!()

...> rescue

...>  OnePiece.Result.ErrUnwrapError -> "was a unwrap failure"

...> end

"was a unwrap failure"

```

[Link to this function](#unwrap_ok/2 "Link to this function")

unwrap\_ok(arg, on\_error)

==========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L364 "View Source")

```

@spec unwrap_ok(result :: [t](#t:t/0)(), on_error :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Returns the contained [`ok/0`](#t:ok/0) value or a provided default.

```

iex> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.unwrap\_ok(21)

42

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.unwrap\_ok(21)

21

```

> #### Lazy Evaluation

> 

> It is recommended to pass a function as the default value, which is lazily evaluated.

> 

> 

```

iex> say\_hello\_world = fn \_x -> "hello, world!" end

...> 42

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.unwrap\_ok(say\_hello\_world)

42

iex> say\_hello\_world = fn \_x -> "hello, world!" end

...> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.unwrap\_ok(say\_hello\_world)

"hello, world!"

```

[Link to this function](#unwrap_ok!/1 "Link to this function")

unwrap\_ok!(arg)

================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L390 "View Source")

```

@spec unwrap_ok!(result :: [t](#t:t/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() | [no\_return](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

Unwrap an [`ok/0`](#t:ok/0) result, or raise an exception.

```

iex> try do

...>  42

...>  |> OnePiece.Result.ok()

...>  |> OnePiece.Result.unwrap\_ok!()

...> rescue

...>  OnePiece.Result.OkUnwrapError -> "was a unwrap failure"

...> end

42

iex> try do

...>  "oops"

...>  |> OnePiece.Result.err()

...>  |> OnePiece.Result.unwrap\_ok!()

...> rescue

...>  OnePiece.Result.OkUnwrapError -> "was a unwrap failure"

...> end

"was a unwrap failure"

```

[Link to this function](#when_err/2 "Link to this function")

when\_err(resp, on\_err)

========================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L482 "View Source")

```

@spec when_err(result :: [t](#t:t/0)(), on_err :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [t](#t:t/0)()) | [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [ok](#t:ok/0)() | [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Applies a function or returns the value if the result is [`err/0`](#t:err/0), otherwise returns the [`ok/0`](#t:ok/0) value.

```

iex> "something wrong happened"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.when\_err("ooops")

"ooops"

iex> 2

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.when\_err("ooops")

{:ok, 2}

```

You can also pass a function to achieve lazy evaluation:

```

iex> failure = fn \_error -> "lazy ooops" end

...> "something wrong happened"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.when\_err(failure)

"lazy ooops"

```

[Link to this function](#when_ok/2 "Link to this function")

when\_ok(error, on\_ok)

=======================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/result.ex#L299 "View Source")

```

@spec when_ok(result :: [t](#t:t/0)(), on_ok :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) | [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [err](#t:err/0)() | [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Applies a function or returns the value when a [`ok/0`](#t:ok/0) is given, or propagates the error.

```

iex> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.when\_ok(42)

42

iex> "oops"

...> |> OnePiece.Result.err()

...> |> OnePiece.Result.when\_ok(42)

{:error, "oops"}

```

You can also pass a function to achieve lazy evaluation:

```

iex> meaning\_of\_life = fn x -> x \* 2 end

...> 21

...> |> OnePiece.Result.ok()

...> |> OnePiece.Result.when\_ok(meaning\_of\_life)

42

```





OnePiece.Result.ErrUnwrapError — OnePiece.Result v0.4.0

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/err_unwrap_error.ex#L1 "View Source")

OnePiece.Result.ErrUnwrapError exception

(OnePiece.Result v0.4.0)

===================================================================================================================================================================================================================================================

Error raised when trying to unwrap an [`OnePiece.Result.err/0`](OnePiece.Result.html#t:err/0) result.

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[t()](#t:t/0)

[Functions](#functions)

------------------------

[message(e)](#message/1)

Convert exception to string.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:t/0 "Link to this type")

t()

===

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/err_unwrap_error.ex#L6 "View Source")

```

@type t() :: %OnePiece.Result.ErrUnwrapError{__exception__: true, value: [term](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()}

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#message/1 "Link to this function")

message(e)

==========

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/err_unwrap_error.ex#L14 "View Source")

```

@spec message(error :: [t](#t:t/0)()) :: [String.t](https://hexdocs.pm/elixir/String.html#t:t/0)()

```

Convert exception to string.





OnePiece.Result.ExpectedError — OnePiece.Result v0.4.0

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/expected_error.ex#L1 "View Source")

OnePiece.Result.ExpectedError exception

(OnePiece.Result v0.4.0)

================================================================================================================================================================================================================================================

Error raised when trying to [`OnePiece.Result.expect_ok!/2`](OnePiece.Result.html#expect_ok!/2) or [`OnePiece.Result.expect_err!/2`](OnePiece.Result.html#expect_err!/2).

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[t()](#t:t/0)

[Functions](#functions)

------------------------

[message(exception)](#message/1)

Convert exception to string.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:t/0 "Link to this type")

t()

===

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/expected_error.ex#L6 "View Source")

```

@type t() :: %OnePiece.Result.ExpectedError{

  __exception__: true,

  message: [term](https://hexdocs.pm/elixir/typespecs.html#built-in-types)(),

  value: [term](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

}

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#message/1 "Link to this function")

message(exception)

==================

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/expected_error.ex#L14 "View Source")

```

@spec message(error :: [t](#t:t/0)()) :: [String.t](https://hexdocs.pm/elixir/String.html#t:t/0)()

```

Convert exception to string.





OnePiece.Result.OkUnwrapError — OnePiece.Result v0.4.0

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/ok_unwrap_error.ex#L1 "View Source")

OnePiece.Result.OkUnwrapError exception

(OnePiece.Result v0.4.0)

=================================================================================================================================================================================================================================================

Error raised when trying to unwrap an [`OnePiece.Result.ok/0`](OnePiece.Result.html#t:ok/0) result.

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[t()](#t:t/0)

[Functions](#functions)

------------------------

[message(e)](#message/1)

Convert exception to string.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:t/0 "Link to this type")

t()

===

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/ok_unwrap_error.ex#L6 "View Source")

```

@type t() :: %OnePiece.Result.OkUnwrapError{__exception__: true, reason: [term](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()}

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#message/1 "Link to this function")

message(e)

==========

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/lib/one_piece/ok_unwrap_error.ex#L14 "View Source")

```

@spec message(error :: [t](#t:t/0)()) :: [String.t](https://hexdocs.pm/elixir/String.html#t:t/0)()

```

Convert exception to string.





OnePiece.Result — OnePiece.Result v0.4.0

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/straw-hat-team/beam-monorepo/blob/one_piece_result@v0.4.0/apps/one_piece_result/README.md#L1 "View Source")

OnePiece.Result

=========================================================================================================================================================================

[![Hex.pm](https://img.shields.io/hexpm/v/one_piece_result.svg)](https://hex.pm/packages/one_piece_result)

[![Code Coverage](https://codecov.io/gh/straw-hat-team/one_piece_result/branch/master/graph/badge.svg)](https://codecov.io/gh/straw-hat-team/one_piece_result)

Handles [`OnePiece.Result.t/0`](OnePiece.Result.html#t:t/0) responses. Inspired by Rust `std::result::Result` package.

[documentation](#documentation)

 Documentation

-----------------------------------------------

### 

[references](#references)

 References

* [API Reference](api-reference.html)

[← Previous Page

 

API Reference](api-reference.html)

[Next Page →

 

Changelog](changelog.html)

