Witha
With aspect: Monad chain, like Haskell’s do or Clojure’s cats.core/alet.
Installation
Add witha to your list of dependencies in mix.exs:
def deps do
[
{:witha, "~> 0.1.1"}
]
end
Usage
Witha’s syntax is very similer to Elixir’s with.
import Witha
witha Witha.Error, x1 <- {:ok, 1}, do: x1 + 1
# {:ok, 2}
witha Witha.Error,
[x1 <- {:ok, 1},
x2 <- {:ok, x1 + 1}],
do: x1 + x2
# {:ok, 3}
Witha takes pre-defined aspects Witha.Nilable & Witha.Error.
Witha.Nilable (Maybe) can chain term | nil.
witha Witha.Nilable,
[x1 <- 1,
x2 <- x1 + 1],
do: x1 + x2
# 3
witha Witha.Nilable,
[x1 <- nil,
x2 <- x1 + 1],
do: x1 + x2
# nil
Witha.Error (Either) can chain {:ok, term} | {:error, term}.
witha Witha.Error,
[x1 <- {:ok, 1},
x2 <- {:ok, x1 + 1}],
do: x1 + x2
# {:ok, 3}
witha Witha.Error,
[x1 <- {:error, "駄目"},
x2 <- {:ok, x1 + 1}],
do: x1 + x2
# {:error, "駄目"}