Railway
Pipe result tuples {:ok, value} all the way. Noop if the first element differs from :ok
Installation & Usage
Simply add railway to your list of dependencies in your mix.exs:
def deps do
[
{:railway, "~> 1.1"}
]
end
run mix deps.get and use Railway in your Module
defmodule MyModule do
use Railway
def my_func do
"https://www.example.com/api"
~>> Req.get()
~>> Map.get(:body)
~>> Jason.decode()
~>> Map.get("results")
end
endHow does ~>> work?
The ~>> operator introduces the value of an {:ok, value} tuple on the left-hand side
as the first argument to the function call on the right-hand side.
If the tuple on the left-hand side is anything different from {:ok, value} like {:error, reason}
the ~>> operator will just return the left-hand side and not execute the function call on the right.
If the expression on the left-hand side is not a tuple, the ~>> operator will behave just like |> does.
Documentation can be found at https://hexdocs.pm/railway