Lonely v0.1.0 Lonely.Result View Source

Composes the tuples {:ok, a} and {:error, e} as the type Result.t.

It is common to get either a value or nil. A way to apply a function to a value letting nil untouched. The following example will return 20 when n = 2 and nil when n > 3.

import Lonely.Result

[1, 2, 3]
|> Enum.find(fn x -> x == n end)
|> wrap()
|> map(fn x -> x * 10 end)
|> unwrap()

Some functions return {:ok, a} or :error. The next example uses wrap/1 to normalise it to a result tagging the error appropriately. The following example will return 30 when i = 2 and :out_of_bounds when i > 2.

import Lonely.Result

[1, 2, 3]
|> Enum.fetch(i)
|> wrap(with: :out_of_bounds)
|> map(fn x -> x * 10 end)
|> unwrap()

Some other times you are certain you will be receiving a result so you can leave out wrap/1 and just map over the value. Also, you might want to recover from the error.

iex> import Lonely.Result
...> str = "23:50:07"
...> Time.from_iso8601(str)
...> |> map(&Time.to_erl/1)
...> |> flat_map_error(fn
...>   :invalid_format -> {:ok, {0, 0, 0}}
...>   reason -> {:error, {reason, str}}
...> end)
{:ok, {23, 50, 7}}

iex> import Lonely.Result
...> str = "10:11:61"
...> Time.from_iso8601(str)
...> |> map(&Time.to_erl/1)
...> |> flat_map_error(fn
...>   :invalid_format -> {:ok, {0, 0, 0}}
...>   reason -> {:error, {reason, str}}
...> end)
{:error, {:invalid_time, "10:11:61"}}

Link to this section Summary

Types

t()

Result type

Functions

Maps an ok value over a function and flattens the resulting value into a single result

Maps an error value over function

Checks if the result is an error

Checks if the result is ok

Maps an ok value over a function

Maps an error value over function

Returns the value of the result

Returns the value of an ok result or raises the error

Wraps a value into a result

Wraps a value into a result or use the provided error instead of nil

Link to this section Types

Link to this type t() View Source
t() :: {:ok, a} | {:error, e}

Result type.

Link to this section Functions

Link to this function flat_map(arg, f) View Source
flat_map(t, (a -> t)) :: t

Maps an ok value over a function and flattens the resulting value into a single result.

iex> import Lonely.Result
...> flat_map({:ok, 1}, fn x -> {:ok, x} end)
{:ok, 1}

iex> import Lonely.Result
...> flat_map({:error, :boom}, fn x -> {:ok, x} end)
{:error, :boom}
Link to this function flat_map_error(arg, f) View Source
flat_map_error(t, (a -> b)) :: t

Maps an error value over function.

iex> import Lonely.Result
...> flat_map_error({:ok, 1}, fn x -> to_string(x) end)
{:ok, 1}

iex> import Lonely.Result
...> flat_map_error({:error, :boom}, fn _ -> {:ok, -1} end)
{:ok, -1}
Link to this function is_error(a) View Source
is_error(t) :: boolean

Checks if the result is an error.

iex> import Lonely.Result
...> is_error({:ok, 1})
false

iex> import Lonely.Result
...> is_error({:error, 1})
true
Link to this function is_ok(arg) View Source
is_ok(t) :: boolean

Checks if the result is ok.

iex> import Lonely.Result
...> is_ok({:ok, 1})
true

iex> import Lonely.Result
...> is_ok({:error, 1})
false
Link to this function map(arg1, f) View Source
map(t, {:ok, (a -> b)}) :: t
map(t, (a -> b)) :: t

Maps an ok value over a function.

Identity

iex> import Lonely.Result
...> map({:ok, 1}, &(&1))
{:ok, 1}

iex> import Lonely.Result
...> map({:error, :boom}, &(&1))
{:error, :boom}

Composition

iex> import Lonely.Result
...> f = fn x -> x + x end
...> g = fn x -> x - x end
...> {:ok, 1}
...> |> map(f)
...> |> map(g)
{:ok, 0}

iex> import Lonely.Result
...> f = fn x -> x + x end
...> g = fn x -> x - x end
...> fg = &(&1 |> f.() |> g.())
...> map({:ok, 1}, fg)
{:ok, 0}

Applicative

iex> import Lonely.Result
...> a = {:ok, 1}
...> f = {:ok, fn x -> x + x end}
...> map(a, f)
{:ok, 2}

iex> import Lonely.Result
...> e = {:error, :boom}
...> f = {:ok, fn x -> x + x end}
...> map(e, f)
{:error, :boom}

iex> import Lonely.Result
...> a = {:ok, 1}
...> e = {:error, :boom}
...> map(a, e)
{:error, :boom}
Link to this function map_error(arg, f) View Source
map_error(t, (a -> b)) :: t

Maps an error value over function.

iex> import Lonely.Result
...> map_error({:ok, 1}, fn x -> to_string(x) end)
{:ok, 1}

iex> import Lonely.Result
...> map_error({:error, :boom}, fn x -> to_string(x) end)
{:error, "boom"}
Link to this function unwrap(arg) View Source
unwrap(t) :: a

Returns the value of the result.

If you want to raise the error, use unwrap!/1.

iex> import Lonely.Result
...> unwrap({:ok, 1})
1

iex> import Lonely.Result
...> unwrap({:error, :boom})
:boom
Link to this function unwrap!(arg) View Source
unwrap!(t) :: a

Returns the value of an ok result or raises the error.

iex> import Lonely.Result
...> unwrap!({:ok, 1})
1

iex> import Lonely.Result
...> assert_raise(RuntimeError, "boom", fn -> unwrap!({:error, "boom"}) end)
%RuntimeError{message: "boom"}

Wraps a value into a result.

iex> import Lonely.Result
...> wrap(nil)
{:error, nil}

iex> import Lonely.Result
...> wrap({:error, :boom})
{:error, :boom}

iex> import Lonely.Result
...> wrap({:ok, 1})
{:ok, 1}

iex> import Lonely.Result
...> wrap(1)
{:ok, 1}
Link to this function wrap(a, list) View Source
wrap(a, [{:with, e}]) :: t

Wraps a value into a result or use the provided error instead of nil.

iex> import Lonely.Result
...> wrap(nil, with: :boom)
{:error, :boom}