moonsugar v0.1.2 Moonsugar.Result
The Result module contains functions that help create and interact with the result type. The result type is represented as either {:ok, value}
or {:error, reason}
.
Link to this section Summary
Functions
Attempts to do something that might throw an error, and converts the output to a result type
Like map, but chain takes a function that returns a result type. This prevents nested results
Helper function to create a error tuple
Converts a variable from a maybe type to a result type
Converts a variable that might be nil to a result type
Converts a variable from a validation type to a result type
Extracts a value from a result type. If the provided argument doesn’t have a value, the default is returned
Determines if a value is a result type
Maps over a result type
Helper function to create an ok tuple
Link to this section Functions
Attempts to do something that might throw an error, and converts the output to a result type.
Examples
iex> Result.attempt("Shoot Elf")
{:ok, "Shoot Elf"}
iex> Result.attempt(raise("You Died"))
{:error, "You Died"}
Like map, but chain takes a function that returns a result type. This prevents nested results.
Examples
iex> Result.map({:ok, 3}, fn(x) ->
...> cond do
...> x > 0 -> {:ok, x * 3}
...> x <= 0 -> {:error, "number less than 1"}
...> end
...> end)
{:ok, {:ok, 9}}
iex> Result.chain({:ok, 3}, fn(x) ->
...> cond do
...> x > 0 -> {:ok, x * 3}
...> x <= 0 -> {:error, "number less than 1"}
...> end
...> end)
{:ok, 9}
iex> Result.chain({:ok, 0}, fn(x) ->
...> cond do
...> x > 0 -> {:ok, x * 3}
...> x <= 0 -> {:error, "number less than 1"}
...> end
...> end)
{:error, "number less than 1"}
iex> Result.chain({:error, "no number found"}, fn(x) ->
...> cond do
...> x > 0 -> {:ok, x * 3}
...> x <= 0 -> {:error, "number less than 1"}
...> end
...> end)
{:error, "no number found"}
Helper function to create a error tuple.
Examples
iex> Result.error("Goat is floating")
{:error, "Goat is floating"}
Converts a variable from a maybe type to a result type.
Examples
iex> Result.from_maybe({:just, 3}, "Not a number")
{:ok, 3}
iex> Result.from_maybe(:nothing, "Not a number")
{:error, "Not a number"}
Converts a variable that might be nil to a result type.
Examples
iex> Result.from_nilable("khajiit has wares", "khajiit does not have wares")
{:ok, "khajiit has wares"}
iex> Result.from_nilable(nil, "khajiit does not have wares")
{:error, "khajiit does not have wares"}
Converts a variable from a validation type to a result type.
Examples
iex> Result.from_validation({:success, "Dragon Slayed"})
{:ok, "Dragon Slayed"}
iex> Result.from_validation({:fail, ["You Died", "You ran out of mana"]})
{:error, ["You Died", "You ran out of mana"]}
Extracts a value from a result type. If the provided argument doesn’t have a value, the default is returned.
Examples
iex> Result.get_with_default({:ok, 3}, 0)
3
iex> Result.get_with_default({:error, "Game Crashed"}, 0)
0
Determines if a value is a result type.
## Examples
iex> Result.is_result({:ok, 0})
true
iex> Result.is_result({:error, "Not enough ore"})
true
iex> Result.is_result({:just, 3})
false
Maps over a result type.
Examples
iex> Result.map({:ok, 3}, fn(x) -> x * 2 end)
{:ok, 6}
iex> Result.map({:error, "Dwarves"}, fn(x) -> x * 2 end)
{:error, "Dwarves"}
Helper function to create an ok tuple.
Examples
iex> Result.ok(3)
{:ok, 3}