Kaur v1.1.0 Kaur.Result View Source
Utilities for working with “result tuples”.
{:ok, value}
{:error, reason}
Link to this section Summary
Functions
Calls the next function only if it receives an ok tuple. Otherwise it skips the call and returns the error tuple
Calls the first function if it receives an error tuple, and the second one if it receives an ok tuple
Creates a new error result tuple
Checks if a result_tuple
is an error
Promotes any value to a result tuple. It excludes nil
for the
ok tuples
Converts an Ok
value to an Error
value if the predicate
is not valid
Calls the next function only if it receives an ok tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an ok tuple
Calls the next function only if it receives an error tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an error tuple
Creates a new ok result tuple
Checks if a result_tuple
is ok
Calls the next function only if it receives an error tuple. Otherwise it skips the call and returns the ok tuple. It expects the function to return a new result tuple
Converts an Ok
value to an Error
value if the predicate
is valid
Transforms a list of result tuple to a result tuple containing either the first error tuple or an ok tuple containing the list of values
Calls the next function only if it receives an ok tuple but discards the result. It always returns the original tuple
Calls the next function only if it receives an error tuple but discards the result. It always returns the original tuple
Returns the content of an ok tuple if the value is correct. Otherwise it returns the default value
Link to this section Types
Link to this section Functions
and_then(result_tuple, (any -> result_tuple)) :: result_tuple
Calls the next function only if it receives an ok tuple. Otherwise it skips the call and returns the error tuple.
Examples
iex> business_logic = fn x -> Kaur.Result.ok(x * 2) end
...> 21 |> Kaur.Result.ok |> Kaur.Result.and_then(business_logic)
{:ok, 42}
iex> business_logic = fn x -> Kaur.Result.ok(x * 2) end
...> "oops" |> Kaur.Result.error |> Kaur.Result.and_then(business_logic)
{:error, "oops"}
either(result_tuple, (any -> any), (any -> any)) :: any
Calls the first function if it receives an error tuple, and the second one if it receives an ok tuple.
Examples
iex> on_ok = fn x -> "X is #{x}" end
...> on_error = fn e -> "Error: #{e}" end
...> 42 |> Kaur.Result.ok |> Kaur.Result.either(on_error, on_ok)
"X is 42"
iex> on_ok = fn x -> "X is #{x}" end
...> on_error = fn e -> "Error: #{e}" end
...> "oops" |> Kaur.Result.error |> Kaur.Result.either(on_error, on_ok)
"Error: oops"
Checks if a result_tuple
is an error.
Examples
iex> 1 |> Kaur.Result.ok |> Kaur.Result.error?
false
iex> 2 |>Kaur.Result.error |> Kaur.Result.error?
true
Promotes any value to a result tuple. It excludes nil
for the
ok tuples.
Examples
iex> Kaur.Result.from_value(nil)
{:error, :no_value}
iex> Kaur.Result.from_value(42)
{:ok, 42}
keep_if(result_tuple, (any -> boolean), any) :: result_tuple
Converts an Ok
value to an Error
value if the predicate
is not valid.
Examples
iex> res = Kaur.Result.ok(10)
...> Kaur.Result.keep_if(res, &(&1 > 5))
{:ok, 10}
iex> res = Kaur.Result.ok(10)
...> Kaur.Result.keep_if(res, &(&1 > 10), "must be > of 10")
{:error, "must be > of 10"}
iex> res = Kaur.Result.error(:no_value)
...> Kaur.Result.keep_if(res, &(&1 > 10), "must be > of 10")
{:error, :no_value}
map(result_tuple, (any -> any)) :: result_tuple
Calls the next function only if it receives an ok tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an ok tuple.
Examples
iex> business_logic = fn x -> x * 2 end
...> 21 |> Kaur.Result.ok |> Kaur.Result.map(business_logic)
{:ok, 42}
iex> business_logic = fn x -> x * 2 end
...> "oops" |> Kaur.Result.error |> Kaur.Result.map(business_logic)
{:error, "oops"}
map_error(result_tuple, (any -> any)) :: result_tuple
Calls the next function only if it receives an error tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an error tuple.
Examples
iex> better_error = fn _ -> "A better error message" end
...> 42 |> Kaur.Result.ok |> Kaur.Result.map_error(better_error)
{:ok, 42}
iex> better_error = fn _ -> "A better error message" end
...> "oops" |> Kaur.Result.error |> Kaur.Result.map_error(better_error)
{:error, "A better error message"}
Checks if a result_tuple
is ok.
Examples
iex> 1 |> Kaur.Result.ok |> Kaur.Result.ok?
true
iex> 2 |> Kaur.Result.error |>Kaur.Result.ok?
false
or_else(result_tuple, (any -> result_tuple)) :: result_tuple
Calls the next function only if it receives an error tuple. Otherwise it skips the call and returns the ok tuple. It expects the function to return a new result tuple.
Examples
iex> business_logic = fn _ -> {:error, "a better error message"} end
...> {:ok, 42} |> Kaur.Result.or_else(business_logic)
{:ok, 42}
iex> business_logic = fn _ -> {:error, "a better error message"} end
...> {:error, "oops"} |> Kaur.Result.or_else(business_logic)
{:error, "a better error message"}
iex> default_value = fn _ -> {:ok, []} end
...> {:error, "oops"} |> Kaur.Result.or_else(default_value)
{:ok, []}
reject_if(result_tuple, (any -> boolean), any) :: result_tuple
Converts an Ok
value to an Error
value if the predicate
is valid.
Examples
iex> res = Kaur.Result.ok([])
...> Kaur.Result.reject_if(res, &Enum.empty?/1)
{:error, :invalid}
iex> res = Kaur.Result.ok([1])
...> Kaur.Result.reject_if(res, &Enum.empty?/1)
{:ok, [1]}
iex> res = Kaur.Result.ok([])
...> Kaur.Result.reject_if(res, &Enum.empty?/1, "list cannot be empty")
{:error, "list cannot be empty"}
sequence([result_tuple]) :: {:ok, [any]} | {:error, any}
Transforms a list of result tuple to a result tuple containing either the first error tuple or an ok tuple containing the list of values.
Examples
iex> Kaur.Result.sequence([Kaur.Result.ok(42), Kaur.Result.ok(1337)])
{:ok, [42, 1337]}
iex> Kaur.Result.sequence([Kaur.Result.ok(42), Kaur.Result.error("oops"), Kaur.Result.ok(1337)])
{:error, "oops"}
tap(result_tuple, (any -> any)) :: result_tuple
Calls the next function only if it receives an ok tuple but discards the result. It always returns the original tuple.
Examples
iex> some_logging = fn x -> IO.puts "Success #{x}" end
...> {:ok, 42} |> Kaur.Result.tap(some_logging)
{:ok, 42}
iex> some_logging = fn _ -> IO.puts "Not called logging" end
...> {:error, "oops"} |> Kaur.Result.tap(some_logging)
{:error, "oops"}
tap_error(result_tuple, (any -> any)) :: result_tuple
Calls the next function only if it receives an error tuple but discards the result. It always returns the original tuple.
Examples
iex> some_logging = fn x -> IO.puts “Failed #{x}” end …> {:error, “oops”} |> Kaur.Result.tap_error(some_logging)
iex> somelogging = fn -> IO.puts “Not called logging” end …> {:ok, 42} |> Kaur.Result.tap_error(some_logging)
with_default(result_tuple, any) :: any
Returns the content of an ok tuple if the value is correct. Otherwise it returns the default value.
Examples
iex> 42 |> Kaur.Result.ok |> Kaur.Result.with_default(1337)
42
iex> "oops" |> Kaur.Result.error |> Kaur.Result.with_default(1337)
1337