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
p()
View Source
p() :: :ok | {:error, any()}
p() :: :ok | {:error, any()}
s(x)
View Source
s(x) :: {:ok, x} | {:error, any()}
s(x) :: {:ok, x} | {:error, any()}
t(x)
View Source
t(x) :: :ok | s(x)
t(x) :: :ok | s(x)
Link to this section Functions
bind(arg, f) View Source (since 0.1.0)
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}
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()
extract!(ma)
View Source
(since 0.1.0)
extract!(s(a)) :: a when a: var
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!
fmap(m, f) View Source (since 0.1.0)
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}
ignore(arg1) View Source (since 0.2.0)
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}
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
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