Maybex v1.0.0 Maybe protocol View Source

Link to this section Summary

Functions

Returns true if the thing is an Error, false otherwise.

Returns true if the thing is not an error, false otherwise.

Applies fun to the value inside an Ok, returning the result lifted into an Ok. Returns an Error untouched if passed one

Like map, but works on the error only. If passed an ok value, passes it through untouched.

Returns the value contained inside the thing, regardless of whether it is an error or not.

Returns the value contained inside the thing, raising if the thing is an error.

Returns the value inside the Ok if passed an Ok, or passes the value inside the Error to the or_else fun and calls it.

Link to this section Types

Link to this section Functions

Returns true if the thing is an Error, false otherwise.

Examples

iex> Maybe.is_error?({:error, nil})
true

iex> Maybe.is_error?({:ok, nil})
false

Returns true if the thing is not an error, false otherwise.

Examples

iex> Maybe.is_ok?({:error, nil})
false

iex> Maybe.is_ok?({:ok, nil})
true

Applies fun to the value inside an Ok, returning the result lifted into an Ok. Returns an Error untouched if passed one

Examples

iex> {:ok, 10} |> Maybe.map(fn x -> x * 10 end)
{:ok, 100}

iex> {:ok, 10} |> Maybe.map(fn x -> {:ok, x * 10} end)
{:ok, 100}

iex> {:error, 10} |> Maybe.map(fn x -> x * 10 end)
{:error, 10}

iex> {:error, 10} |> Maybe.map(fn x -> {:error, x * 10} end)
{:error, 10}

iex> %Error{value: "no"} |> Maybe.map(fn x -> x * 10 end)
%Error{value: "no"}

iex> %Ok{value: 10} |> Maybe.map(fn x -> x * 10 end)
%Ok{value: 100}

Like map, but works on the error only. If passed an ok value, passes it through untouched.

Examples

iex> Maybe.map_error({:error, "Nope"}, fn msg -> msg <> " not at all" end)
{:error, "Nope not at all"}

iex> Maybe.map_error({:ok, "Yes"}, fn msg -> msg <> " not at all" end)
{:ok, "Yes"}

iex> Maybe.map_error({:error, 10}, fn x -> {:error, x * 10} end)
{:error, 100}

iex> Maybe.map_error({:error, 10}, fn _ -> {:ok, "Nope"} end)
{:ok, "Nope"}

Returns the value contained inside the thing, regardless of whether it is an error or not.

Examples

iex> Maybe.unwrap({:ok, 10})
10

iex> Maybe.unwrap({:error, 10})
10

Returns the value contained inside the thing, raising if the thing is an error.

Examples

iex> Maybe.unwrap!({:ok, 10})
10

iex> Maybe.unwrap!({:error, "Fluffed it"})
** (RuntimeError) Error: Fluffed it
Link to this function

unwrap_or_else(thing, or_else)

View Source

Returns the value inside the Ok if passed an Ok, or passes the value inside the Error to the or_else fun and calls it.

Examples

iex> Maybe.unwrap_or_else({:ok, 10}, fn x -> raise x end)
10

iex> Maybe.unwrap_or_else({:error, 10}, fn x -> x + 10 end)
20