Skip to content
/ rail Public

Railway oriented programming toolkit for Elixir

License

Notifications You must be signed in to change notification settings

jechol/rail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rail

test Coverage Status Hex.pm GitHub

Rail is a helper macros for "Railway oriented programming".

It helps you handle error cases at almost no cost with rail, >>>, and def macro.

If you are not comfortable with "Railway oriented programming", see Railway oriented programming

Installation

def deps do
  [
    {:rail, "~> 1.1.0"}
  ]
end

Usage

Basic usage

use Rail introduces new syntax left <- right,

  • which bind value to left when right is {:ok, value} or value
  • or skips entire code block when right is {:error, _} or :error.
defmodule Target do
  use Rail

  def div(num, denom) do
    denom <- check_denom(denom)
    num / denom
  end

  def check_denom(0) do
    {:error, :div_by_zero}
  end

  def check_denom(n) do
    # same with {:ok, n}
    n
  end
end

iex> Calc.div(10, 2)
5.0
iex> Calc.div(10, 0)
{:error, :div_by_zero}

rail/1 is available inside other code blocks.

iex> rail do
...    x <- {:ok, 1}
...    y <- {:ok, 2}
...
...    x + y
...  end
3

left >>> right is similar to |>, but

  • applies right function only when left is value or {:ok, value}.
  • support both function and call expression, which makes it compatible with |>.
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, :div_by_zero} >>> Integer.to_string()
{:error, :div_by_zero}

For more examples, see Rail

LICENSE

MIT

About

Railway oriented programming toolkit for Elixir

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages