Ok Jose

A tiny library to pipe functions that return {:ok, _} or {:error, _} without having you to mess with matching everywhere.

Motivation

A lot of erlang libraries follow the convention of returning {:ok, _} and {:error, _} tuples to denote success/failure.

I just wanted an easy way to pipe functions in a happy path, that is, a pipe that expects {:ok, _} to be returned at each point. If an non-ok thing is found at any point it breaks the rest of the chain execution and it's returned as result.

So, for example, the following code

filename
|> File.read()
|> case do
{:ok, content} ->
content |> Poison.Parser.parse()
{:error, _} = error -> error
end

can be written as:

filename |> File.read |> Poison.Parser.parse |> ok

or alternatively:

ok(filename |> File.read |> Poison.Parser.parse)

Usage

Just import OkJose, it will provide an ok/1 macro that you can pipe to.

Example

def dup(x), do: {:ok, x * 2}
def nop(x), do: {:error, x}
12 |> dup |> dup |> ok # => {:ok, 48}
24 |> nop |> dup |> ok # => {:error, 24}
24 |> dup |> ok! # => 48
24 |> nop |> dup |> ok! # raises

Installation

  1. Add ok to your list of dependencies in mix.exs:
def deps do
[{:ok_jose, "~> 0.0.1"}]
end

About ok

I wanted name this library ok, but the hex package name was already taken. So I just wanted to make a tribute to @josevalim.

Actually both projects are trying to solve the same issue. But I think this one has an easier syntax that consist of just piping to ok

Is it any good?

Yes