rop v0.5.3 Rop

Summary

Functions

Extracts the value from a tagged tuple like {:ok, value} Raises the value from a tagged tuple like {:error, value} Raise the arguments else

Macros

No need to stop pipelining in case of an error somewhere in the middle

Wraps a simple function to return a tagged tuple with :ok to comply to the protocol {:ok, result}

Like a similar Unix utility it does some work and returns the input as output. See tee (command), Unix)

Wraps raising functions to return a tagged tuple {:error, ErrorMessage} to comply with the protocol

Functions

ok(x)

Extracts the value from a tagged tuple like {:ok, value} Raises the value from a tagged tuple like {:error, value} Raise the arguments else

For example:

iex> ok({:ok, 1})
1

iex> ok({:error, "some"})
** (RuntimeError) some

iex> ok({:anything, "some"})
** (ArgumentError) raise/1 expects an alias, string or exception as the first argument, got: {:anything, "some"}

Macros

left >>> right

No need to stop pipelining in case of an error somewhere in the middle

Example:

iex> inc = fn(x)-> {:ok, x+1} end
iex> 1 |> (inc).() >>> (inc).()
{:ok, 3}
bind(args, func)

Wraps a simple function to return a tagged tuple with :ok to comply to the protocol {:ok, result}

Example:

iex> 1 |> Integer.to_string
"1"
iex> 1 |> bind(Integer.to_string)
{:ok, "1"}


iex> inc = fn(x)-> x+1 end
iex> 1 |> bind((inc).()) >>> (inc).()
3
iex> 1 |> bind((inc).()) >>> bind((inc).())
{:ok, 3}
tee(args, func)

Like a similar Unix utility it does some work and returns the input as output. See tee (command), Unix).

Example:

iex> inc = fn(x)-> IO.inspect(x); {:ok, x + 1} end
iex> 1 |> tee((inc).()) >>> tee((inc).()) >>> tee((inc).())
{:ok, 1}
try_catch(args, func)

Wraps raising functions to return a tagged tuple {:error, ErrorMessage} to comply with the protocol

Example:

iex> r = fn(_)-> raise "some" end
iex> inc = fn(x)-> x + 1 end
iex> 1 |> bind((inc).()) >>> try_catch((r).()) >>> bind((inc).())
{:error, %RuntimeError{message: "some"}}