View Source Rail (rail v0.7.0)

Link to this section Summary

Functions

Apply a function or pipe to a function call when value is not {:error, _} or :error

Apply a function when value is not {:error, _} or :error

Apply a function for error of {:error, error}, otherwise bypass

Apply a function for value of {:ok, value}, otherwise bypass

Link to this section Functions

Apply a function or pipe to a function call when value is not {:error, _} or :error

examples

Examples

iex> 1 >>> fn v -> Integer.to_string(v) end
"1"
iex> {:ok, 1} >>> fn v -> Integer.to_string(v) end
"1"
iex> {:ok, 1} >>> Integer.to_string()
"1"
iex> :error >>> Integer.to_string()
:error
iex> {:error, :div_by_zero} >>> Integer.to_string()
{:error, :div_by_zero}
@spec chain(any(), (any() -> any())) :: any()

Apply a function when value is not {:error, _} or :error

examples

Examples

iex> 1 |> Rail.chain(fn v -> v + 10 end)
11
iex> {:ok, 1} |> Rail.chain(fn v -> v + 10 end)
11
iex> :error |> Rail.chain(fn v -> v + 10 end)
:error
iex> {:error, :noent} |> Rail.chain(fn v -> v + 10 end)
{:error, :noent}
Link to this macro

def(head, body)

View Source (macro)
Link to this macro

defp(head, body)

View Source (macro)

Apply a function for error of {:error, error}, otherwise bypass

examples

Examples

iex> :error |> Rail.map_error(fn :noent -> :not_found end)
:error
iex> {:error, :noent} |> Rail.map_error(fn :noent -> :not_found end)
{:error, :not_found}

Apply a function for value of {:ok, value}, otherwise bypass

examples

Examples

iex> :ok |> Rail.map_ok(fn 1 -> 10 end)
:ok
iex> {:ok, 1} |> Rail.map_ok(fn 1 -> 10 end)
{:ok, 10}
iex> {:error, 1} |> Rail.map_ok(fn 1 -> 10 end)
{:error, 1}
Link to this macro

rail(head, body)

View Source (macro)
Link to this macro

railp(head, body)

View Source (macro)