Lonely v0.3.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
Functions
Filters a result value
Fits a tagged tuple into a Result
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
Result type.
Link to this section Functions
Filters a result value.
iex> import Lonely.Result
...> filter_map({:ok, 1}, fn x -> x == 1 end, fn x -> x + x end)
{:ok, 2}
iex> import Lonely.Result
...> filter_map({:ok, 1}, fn x -> x == 2 end, fn x -> x + x end)
{:ok, 1}
iex> import Lonely.Result
...> filter_map({:error, :boom}, fn x -> x == 1 end, fn x -> x + x end)
{:error, :boom}
Fits a tagged tuple into a Result.
iex> import Lonely.Result
...> fit(:ok)
{:ok, nil}
iex> import Lonely.Result
...> fit({:ok, 1})
{:ok, 1}
iex> import Lonely.Result
...> fit({:ok, 1, 2})
{:ok, {1, 2}}
iex> import Lonely.Result
...> "2017-10-11T12:13:14Z" |> DateTime.from_iso8601() |> fit()
{:ok, {%DateTime{calendar: Calendar.ISO,
day: 11,
hour: 12,
microsecond: {0, 0},
minute: 13,
month: 10,
second: 14,
std_offset: 0,
time_zone: "Etc/UTC",
utc_offset: 0,
year: 2017,
zone_abbr: "UTC"}, 0}}
iex> import Lonely.Result
...> fit({:ok, 1, 2, 3})
{:ok, {1, 2, 3}}
iex> import Lonely.Result
...> fit({:ok, 1, 2, 3, 4})
{:ok, {1, 2, 3, 4}}
iex> import Lonely.Result
...> fit({:ok, 1, 2, 3, 4, 5})
{:ok, {1, 2, 3, 4, 5}}
iex> import Lonely.Result
...> fit(:error)
{:error, nil}
iex> import Lonely.Result
...> fit({:error, 1})
{:error, 1}
iex> import Lonely.Result
...> fit(nil)
{:error, nil}
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}
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}
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
Checks if the result is ok.
iex> import Lonely.Result
...> is_ok({:ok, 1})
true
iex> import Lonely.Result
...> is_ok({:error, 1})
false
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}
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"}
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
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}
Wraps a value into a result or use the provided error instead of nil
.
iex> import Lonely.Result
...> wrap(nil, with: :boom)
{:error, :boom}