Packages

Versioce provides a mix task for version bumping of your project.

Current section

Files

Jump to
versioce lib Utils ok_monad.ex
Raw

lib/Utils/ok_monad.ex

defmodule Versioce.OK do
@moduledoc false
@type t :: {:ok, any} | {:error, any}
@doc """
Wraps and unwraps the value in ok tuple.
"""
@spec unit(any) :: t()
def unit({:ok, _} = value), do: value
def unit({:error, _} = value), do: value
def unit(value), do: {:ok, value}
@doc """
Bind operator.
Runs a given func against the ok_tuple.
If its the error tuple -> skip evaluation
"""
defmacro left ~>> right do
quote do
unquote(left)
|> Versioce.OK.unit()
|> case do
{:ok, value} -> value |> unquote(right)
{:error, reason} -> {:error, reason}
end
end
end
end