ExOption (ex_option v0.1.0)
Option library inspired by Rust.
Link to this section Summary
Functions
Returns none if the arg1 is none, otherwise returns arg2.
Returns none if the option is none, otherwise calls fun with the wrapped value and returns the result.
Returns none if the option is none, otherwise calls fun with the wrapped value and returns
Converts from nested option to unnested option.
Maps an option to another option by applying a function to a contained value.
Applies a function to the contained value (if any), or returns the provided default (if not).
No value.
Returns true if the option is a none value.
Returns the arg1 if it contains a value, otherwise returns arg2.
Replaces the actual value in the option by the value given in parameter.
Some value.
Returns true if the option is a some value.
Returns the contained some value.
Returns the contained some value or a provided default.
Returns some if exactly one of arg1, arg2 is Some, otherwise returns none.
Zips arg1 with arg2.
Link to this section Types
option()
Specs
option() :: {:some, any()} | {:none}
Link to this section Functions
and_option(arg1, arg2)
Specs
Returns none if the arg1 is none, otherwise returns arg2.
Examples
iex> ExOption.some(2) |> ExOption.and_option(ExOption.none())
{:none}
iex> ExOption.none() |> ExOption.and_option(ExOption.some("foo"))
{:none}
iex> ExOption.some(2) |> ExOption.and_option(ExOption.some("foo"))
{:some, "foo"}
iex> ExOption.none() |> ExOption.and_option(ExOption.none())
{:none}
and_then(arg, fun)
Specs
Returns none if the option is none, otherwise calls fun with the wrapped value and returns the result.
Examples
iex> ExOption.some(2) |> ExOption.and_then(fn x -> ExOption.some(x * x) end) |> ExOption.and_then(fn x -> ExOption.some(x * x) end)
{:some, 16}
iex> ExOption.some(2) |> ExOption.and_then(fn x -> ExOption.some(x * x) end) |> ExOption.and_then(fn _ -> ExOption.none() end)
{:none}
iex> ExOption.some(2) |> ExOption.and_then(fn _ -> ExOption.none() end) |> ExOption.and_then(fn x -> ExOption.some(x * x) end)
{:none}
iex> ExOption.none() |> ExOption.and_then(fn x -> ExOption.some(x * x) end) |> ExOption.and_then(fn x -> ExOption.some(x * x) end)
{:none}
filter(arg, fun)
Specs
Returns none if the option is none, otherwise calls fun with the wrapped value and returns:
- some if fun returns true, and
- none if fun returns false.
Examples
iex> ExOption.none() |> ExOption.filter(fn n -> rem(n, 2) == 0 end)
{:none}
iex> ExOption.some(3) |> ExOption.filter(fn n -> rem(n, 2) == 0 end)
{:none}
iex> ExOption.some(4) |> ExOption.filter(fn n -> rem(n, 2) == 0 end)
{:some, 4}
flatten(value)
Specs
Converts from nested option to unnested option.
Examples
iex> ExOption.some(ExOption.some(6)) |> ExOption.flatten()
{:some, 6}
iex> ExOption.some(ExOption.none()) |> ExOption.flatten()
{:none}
iex> ExOption.none() |> ExOption.flatten()
{:none}
map(arg, fun)
Specs
Maps an option to another option by applying a function to a contained value.
Examples
iex> ExOption.some("Hello, World!") |> ExOption.map(&String.length/1)
{:some, 13}
map_or(arg, default, fun)
Specs
Applies a function to the contained value (if any), or returns the provided default (if not).
Examples
iex> ExOption.some("foo") |> ExOption.map_or(42, &String.length/1)
3
iex> ExOption.none() |> ExOption.map_or(42, &String.length/1)
42
none()
Specs
none() :: option()
No value.
none?(arg)
Specs
Returns true if the option is a none value.
Examples
iex> ExOption.some(2) |> ExOption.none?()
false
iex> ExOption.none() |> ExOption.none?()
true
or_option(arg1, arg2)
Specs
Returns the arg1 if it contains a value, otherwise returns arg2.
Examples
iex> ExOption.some(2) |> ExOption.or_option(ExOption.none())
{:some, 2}
iex> ExOption.none() |> ExOption.or_option(ExOption.some(100))
{:some, 100}
iex> ExOption.some(2) |> ExOption.or_option(ExOption.some(100))
{:some, 2}
iex> ExOption.none() |> ExOption.or_option(ExOption.none())
{:none}
replace(arg, value)
Specs
Replaces the actual value in the option by the value given in parameter.
Returning the old value if present, leaving a some in its place without deinitializing either one.
Examples
iex> ExOption.some(2) |> ExOption.replace(5)
{:some, 5}
iex> ExOption.none() |> ExOption.replace(3)
{:none}
some(value)
Specs
Some value.
some?(arg)
Specs
Returns true if the option is a some value.
Examples
iex> ExOption.some(2) |> ExOption.some?()
true
iex> ExOption.none() |> ExOption.some?()
false
unwrap(arg)
Specs
Returns the contained some value.
Errors if the value equals none.
Examples
iex> ExOption.some("air") |> ExOption.unwrap()
"air"
iex> ExOption.none() |> ExOption.unwrap()
** (ArgumentError) the value is none
unwrap_or(arg, default)
Specs
Returns the contained some value or a provided default.
Examples
iex> ExOption.some("car") |> ExOption.unwrap_or("bike")
"car"
iex> ExOption.none() |> ExOption.unwrap_or("bike")
"bike"
xor_option(arg1, arg2)
Specs
Returns some if exactly one of arg1, arg2 is Some, otherwise returns none.
Examples
iex> ExOption.some(2) |> ExOption.xor_option(ExOption.none())
{:some, 2}
iex> ExOption.none() |> ExOption.xor_option(ExOption.some(2))
{:some, 2}
iex> ExOption.some(2) |> ExOption.xor_option(ExOption.some(2))
{:none}
iex> ExOption.none() |> ExOption.xor_option(ExOption.none())
{:none}
zip(arg1, arg2)
Specs
Zips arg1 with arg2.
Examples
iex> ExOption.some(1) |> ExOption.zip(ExOption.some("hi"))
{:some, {1, "hi"}}
iex> ExOption.some(1) |> ExOption.zip(ExOption.none())
{:none}