View Source Rail (rail v1.1.0)
Summary
Functions
Apply a function or pipe to a function call when value is not {:error, _} or :error
Apply a function when value is not {:error, _} or :error
Similar to Kernel.def/2
, but the function body is wrapped in a rail/1
block.
Similar to Kernel.defp/2
, but the function body is wrapped in a rail/1
block.
Return a new tuple for error of {:error, error}, otherwise bypass
Return a new tuple for value of {:ok, value}, otherwise bypass
Apply a function for error of {:error, error}, otherwise bypass
Apply a function for value of {:ok, value}, otherwise bypass
Normalize input to {:ok, value} or {:error, error}
Introduces new syntax left <- right
,
Similar to Kernel.def/2
, but the function body is wrapped in a rail/1
block.
Similar to Kernel.defp/2
, but the function body is wrapped in a rail/1
block.
Functions
Apply a function or pipe to a function call when value is not {:error, _} or :error
Examples
iex> 1 >>> fn v -> Integer.to_string(v) end
"1"
iex> {:ok, 1} >>> fn v -> Integer.to_string(v) end
"1"
iex> {:ok, 1} >>> Integer.to_string()
"1"
iex> :error >>> Integer.to_string()
:error
iex> {:error, :div_by_zero} >>> Integer.to_string()
{:error, :div_by_zero}
Apply a function when value is not {:error, _} or :error
Examples
iex> 1 |> Rail.chain(fn v -> v + 10 end)
11
iex> {:ok, 1} |> Rail.chain(fn v -> v + 10 end)
11
iex> :error |> Rail.chain(fn v -> v + 10 end)
:error
iex> {:error, :noent} |> Rail.chain(fn v -> v + 10 end)
{:error, :noent}
Similar to Kernel.def/2
, but the function body is wrapped in a rail/1
block.
defmodule Target do
use Rail
def sum() do
x <- {:ok, 1}
y <- {:ok, 1}
{:ok, x + y}
end
end
Similar to Kernel.defp/2
, but the function body is wrapped in a rail/1
block.
defmodule Target do
use Rail
defp sum() do
x <- {:ok, 1}
y <- {:ok, 1}
{:ok, x + y}
end
end
Return a new tuple for error of {:error, error}, otherwise bypass
Examples
iex> :error |> Rail.flat_map_error(fn :noent -> {:noreply, :not_found} end)
:error
iex> {:error, :noent} |> Rail.flat_map_error(fn :noent -> {:noreply, :not_found} end)
{:noreply, :not_found}
iex> {:ok, 1} |> Rail.flat_map_error(fn :noent -> {:noreply, :not_found} end)
{:ok, 1}
Return a new tuple for value of {:ok, value}, otherwise bypass
Examples
iex> :ok |> Rail.flat_map_ok(fn 1 -> {:noreply, 10} end)
:ok
iex> {:ok, 1} |> Rail.flat_map_ok(fn 1 -> {:noreply, 10} end)
{:noreply, 10}
iex> {:error, 1} |> Rail.flat_map_ok(fn 1 -> {:noreply, 10} end)
{:error, 1}
Apply a function for error of {:error, error}, otherwise bypass
Examples
iex> :error |> Rail.map_error(fn :noent -> :not_found end)
:error
iex> {:error, :noent} |> Rail.map_error(fn :noent -> :not_found end)
{:error, :not_found}
iex> {:ok, 1} |> Rail.map_error(fn :noent -> :not_found end)
{:ok, 1}
Apply a function for value of {:ok, value}, otherwise bypass
Examples
iex> :ok |> Rail.map_ok(fn 1 -> 10 end)
:ok
iex> {:ok, 1} |> Rail.map_ok(fn 1 -> 10 end)
{:ok, 10}
iex> {:error, 1} |> Rail.map_ok(fn 1 -> 10 end)
{:error, 1}
Normalize input to {:ok, value} or {:error, error}
Examples
iex> :error |> Rail.normalize()
{:error, nil}
iex> {:error, 1, 2} |> Rail.normalize()
{:error, {1, 2}}
iex> :ok |> Rail.normalize()
{:ok, nil}
iex> {:ok, 1, 2} |> Rail.normalize()
{:ok, {1, 2}}
iex> {:hello, :world} |> Rail.normalize()
{:ok, {:hello, :world}}
Introduces new syntax left <- right
,
- which bind
value
to left whenright
is{:ok, value}
or justvalue
- or skips entire code block when
right
is{:error, _}
or:error
.
Examples
iex> rail do
...> x <- {:ok, 1}
...> y <- {:ok, 2}
...>
...> x + y
...> end
3
Similar to Kernel.def/2
, but the function body is wrapped in a rail/1
block.
defmodule Target do
use Rail
rail sum() do
x <- {:ok, 1}
y <- {:ok, 1}
{:ok, x + y}
end
end
Similar to Kernel.defp/2
, but the function body is wrapped in a rail/1
block.
defmodule Target do
use Rail
rail sum() do
x <- {:ok, 1}
y <- {:ok, 1}
{:ok, x + y}
end
end