Brex.Result v0.4.0 Brex.Result.Base View Source

Tools for doing basic result tuple manipulations.

Link to this section Summary

Functions

Takes in a tuple and function from plain value to {:ok, any} | {:error, any}. Applies the function to the value within the ok tuple or propogates the error.

Wraps value in an error tuple Will be inlined at compile time.

Extracts the value or reason from the tuple. Caution: If given an error tuple it raise an exception!

Takes in a tuple and a function from plain value to plain value. Applies the function to the value within the ok tuple or propogates error.

Ignores the value in an ok tuple and just returns :ok. Still shortcircuits on error.

Wraps value in an ok tuple. Will be inlined at compile time.

This is infix bind/2 Has same syntax restrictions as pipe. Defined as a macro for syntatic purposes.

Link to this section Types

Link to this section Functions

Link to this function

bind(arg, f) View Source (since 0.1.0)
bind(s(a), (a -> s(b))) :: s(b) when a: var

Takes in a tuple and function from plain value to {:ok, any} | {:error, any}. Applies the function to the value within the ok tuple or propogates the error.

Examples:

iex> bind({:ok, 1}, fn x -> if x == 1, do: {:ok, 2}, else: {:error, "not_one"} end)
{:ok, 2}

iex> bind({:ok, 4}, fn x -> if x == 1, do: {:ok, 2}, else: {:error, "not_one"} end)
{:error, "not_one"}

iex> bind({:error, 4}, fn x -> if x == 1, do: {:ok, 2}, else: {:error, "not_one"} end)
{:error, 4}
Link to this macro

error(r) View Source (macro) (since 0.1.0)

Wraps value in an error tuple Will be inlined at compile time.

Typespec:

error(any) :: t()
Link to this function

extract!(ma) View Source (since 0.1.0)
extract!(s(a)) :: a when a: var

Extracts the value or reason from the tuple. Caution: If given an error tuple it raise an exception!

Link to this function

fmap(m, f) View Source (since 0.1.0)
fmap(s(a), (a -> b)) :: s(b) when a: var, b: var

Takes in a tuple and a function from plain value to plain value. Applies the function to the value within the ok tuple or propogates error.

Examples:

iex> {:ok, 6}
...> |> fmap(fn x -> x+2 end)
{:ok, 8}

iex> {:error, 6}
...> |> fmap(fn x -> x+2 end)
{:error, 6}
Link to this function

ignore(arg1) View Source (since 0.2.0)
ignore(t()) :: p()

Ignores the value in an ok tuple and just returns :ok. Still shortcircuits on error.

Examples:

iex> {:ok, 2}
...> |> ignore
:ok

iex> :ok
...> |> ignore
:ok

iex> {:error, :not_found}
...> |> ignore
{:error, :not_found}
Link to this macro

ok(val) View Source (macro) (since 0.1.0)

Wraps value in an ok tuple. Will be inlined at compile time.

Typespec:

ok(a) :: s(a) when a: var
Link to this macro

arg ~> fun View Source (macro) (since 0.1.0)

This is infix bind/2 Has same syntax restrictions as pipe. Defined as a macro for syntatic purposes.

Examples:

def sgn(x) do
  if x > 0 do
    {:ok, "pos"}
  else
    {:error, "neg"}
  end
end

def two_args(x, y), do: {:ok, x - y}

{:ok, 1}
~> sgn
= {:ok, "pos"}

{:ok, -3}
~> sgn
= {:error, "neg"}

{:error, 2}
~> sgn
= {:error, 2}

{:ok, 3}
~> two_args(2)
= {:ok, 1}

Typespec:

t(a) ~> (a -> t(b)) :: t(b) when a: var, b: var