View Source Rail (rail v1.0.1)
Link to this section 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.
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.
Link to this section Functions
Apply a function or pipe to a function call when value is not {:error, _} or :error
examples
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
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
Apply a function for error of {:error, error}, otherwise bypass
examples
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}
Apply a function for value of {:ok, value}, otherwise bypass
examples
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
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
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