Aikido
Small composable helpers for working with {:ok, value} | {:error, reason}
result tuples in pipelines. The function names are borrowed from aikido
techniques — they describe the shape of the transformation.
Installation
Add aikido to your list of dependencies in mix.exs:
def deps do
[
{:aikido, "~> 0.1.0"}
]
endUsage
{:ok, 2}
|> Aikido.blend(fn n -> {:ok, n * 5} end)
|> Aikido.blend(fn n -> if n > 5, do: {:error, :too_big}, else: {:ok, n} end)
|> Aikido.redirect(fn _ -> {:ok, 0} end)
|> Aikido.unwrap_or(-1)
# => 0Functions
blend/2— apply a function to the value inside{:ok, v}; pass errors through.redirect/2— recover from or transform an error; pass ok through.flip/1— swap the tag of a result tuple.unwrap_or/2— extract the ok value or fall back to a default (static or function of the reason).
Full docs at https://hexdocs.pm/aikido.
License
MIT