Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

NextPipe

notebooks/next_pipe.livemd

NextPipe

Mix.install([{:next_pipe, "~> 0.1"}])
:ok

Import

import NextPipe
NextPipe

Next

next/2: evaulate the function argument if the first argument matches {:ok, _}

:zero
|> next(fn :zero -> {:ok, :one} end)
|> next(fn :one -> {:ok, :two} end)
|> next(fn :two -> {:ok, :three} end)
# on_error is skipped because there's no error
|> on_error(fn :four -> {:ok, :five} end)
|> dbg()
[notebooks/next_pipe.livemd#cell:7: (file)]
:zero #=> :zero
|> next(fn :zero -> {:ok, :one} end) #=> {:ok, :one}
|> next(fn :one -> {:ok, :two} end) #=> {:ok, :two}
|> next(fn :two -> {:ok, :three} end) #=> {:ok, :three}
|> on_error(fn :four -> {:ok, :five} end) #=> {:ok, :three}

{:ok, :three}
:zero
|> next(fn :zero -> {:ok, :one} end)
|> next(fn :one -> {:ok, :two} end)
|> next(fn :two -> {:error, :three} end)
# the function passed below is never evaluated because there's an error
|> next(fn :nope -> {:ok, :four} end)
# the function below is evaulated because there is an error
|> on_error(fn :three -> {:ok, :five} end)
|> dbg()
[notebooks/next_pipe.livemd#cell:9: (file)]
:zero #=> :zero
|> next(fn :zero -> {:ok, :one} end) #=> {:ok, :one}
|> next(fn :one -> {:ok, :two} end) #=> {:ok, :two}
|> next(fn :two -> {:error, :three} end) #=> {:error, :three}
|> next(fn :nope -> {:ok, :four} end) #=> {:error, :three}
|> on_error(fn :three -> {:ok, :five} end) #=> {:ok, :five}

{:ok, :five}

Try Next

try_next/3: evaulate the function argument if the first argument matches {:ok, _} and provide an outlet for exceptions

:zero
|> try_next(fn :zero -> {:ok, :one} end)
|> try_next(fn :one -> {:ok, :two} end)
|> try_next(fn :two -> raise "three" end)
# The function below is skipped because there's an error with the caught exception
|> try_next(fn :nope -> {:ok, :four} end)
|> on_error(fn %RuntimeError{message: "three"} -> {:ok, :five} end)
|> dbg()
[notebooks/next_pipe.livemd#cell:8: (file)]
:zero #=> :zero
|> try_next(fn :zero -> {:ok, :one} end) #=> {:ok, :one}
|> try_next(fn :one -> {:ok, :two} end) #=> {:ok, :two}
|> try_next(fn :two -> raise "three" end) #=> {:error, %RuntimeError{message: "three"}}
|> try_next(fn :nope -> {:ok, :four} end) #=> {:error, %RuntimeError{message: "three"}}
|> on_error(fn %RuntimeError{message: "three"} -> {:ok, :five} end) #=> {:ok, :five}

{:ok, :five}
:zero
|> try_next(fn :zero -> {:ok, :one} end)
|> try_next(fn :one -> {:ok, :two} end)
# Override the rescue function and return an `{:ok, _}` tuple
|> try_next(fn :two -> raise "three" end, fn :two, %RuntimeError{message: "three"} ->
  {:ok, :three}
end)
|> try_next(fn :three -> {:ok, :four} end)
# The function below is skipped because the exception was rescued
|> on_error(fn %RuntimeError{message: "three"} -> {:ok, :five} end)
|> dbg()
[notebooks/next_pipe.livemd#cell:11: (file)]
:zero #=> :zero
|> try_next(fn :zero -> {:ok, :one} end) #=> {:ok, :one}
|> try_next(fn :one -> {:ok, :two} end) #=> {:ok, :two}
|> try_next(fn :two -> raise "three" end, fn :two, %RuntimeError{message: "three"} ->
  {:ok, :three}
end) #=> {:ok, :three}
|> try_next(fn :three -> {:ok, :four} end) #=> {:ok, :four}
|> on_error(fn %RuntimeError{message: "three"} -> {:ok, :five} end) #=> {:ok, :four}

{:ok, :four}

OK

ok/2: an alias for next/2

:zero
|> ok(fn :zero -> {:ok, :one} end)
|> ok(fn :one -> {:ok, :two} end)
|> ok(fn :two -> {:error, :three} end)
|> ok(fn :nope -> {:ok, :four} end)
|> on_error(fn :three -> {:ok, :five} end)
|> dbg()
[notebooks/next_pipe.livemd#cell:7: (file)]
:zero #=> :zero
|> ok(fn :zero -> {:ok, :one} end) #=> {:ok, :one}
|> ok(fn :one -> {:ok, :two} end) #=> {:ok, :two}
|> ok(fn :two -> {:error, :three} end) #=> {:error, :three}
|> ok(fn :nope -> {:ok, :four} end) #=> {:error, :three}
|> on_error(fn :three -> {:ok, :five} end) #=> {:ok, :five}

{:ok, :five}