Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 1.98 KB

README.md

File metadata and controls

64 lines (49 loc) · 1.98 KB

ResultEx

ResultEx is a module for handling functions returning a t:ResultEx.t/0. This module is inspired by the f# Result module, and Railway Oriented Programming as explained by Scott Wlaschin.

A result can be either the tuple {:ok, term} where term will be the expected return value of a function, or the tuple {:error, term} where term will be an explanation of what went wrong while executing a function.

Using this module, it will be possible to combine functions that return a t:ResultEx.t/0, and functions that take the value contained by the ok variant. In the case one of the functions returns an error variant, subsequent functions expecting an ok result can be prevented from being executed. Also, functions can be connected that will only execute in the case of an error.

Examples

defmodule ResultExExample do

  def divide(0, _), do: {:error, :zero_division_exception}
  def divide(0.0, _), do: {:error, :zero_division_exception}
  def divide(x, y), do: ResultEx.return(x / y)
  def subtract(x, y), do: ResultEx.return(x - y)

end

ResultExExample.divide(4, 2)
|> ResultEx.bind(fn x -> ResultExExample.subtract(x, 2) end)
{ :ok, 0.0}
ResultExExample.divide(4, 2)
|> ResultEx.bind(fn x -> ResultExExample.subtract(x, 2) end)
|> ResultEx.bind(fn x -> ResultExExample.divide(x, 2) end)
|> ResultEx.bind(fn x -> ResultExExample.subtract(x, 2) end)
{:error, :zero_division_exception}
iex> ResultExExample.divide(0, 2)
...> |> ResultEx.or_else(2)
2
ResultExExample.divide(0, 2)
|> ResultEx.or_else_with(fn _err -> {:ok, 0} end)
{:ok, 0}

Installation

Add result_ex to your list of dependencies in mix.exs:

def deps do
  [
    {:result_ex, "~> 0.1.0"}
  ]
end