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

Specs

option() :: {:some, any()} | {:none}

Link to this section Functions

Link to this function

and_option(arg1, arg2)

Specs

and_option(option(), option()) :: option()

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}
Link to this function

and_then(arg, fun)

Specs

and_then(option(), (... -> any())) :: option()

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}
Link to this function

filter(arg, fun)

Specs

filter(option(), (... -> any())) :: option()

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}

Specs

flatten(option()) :: option()

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}

Specs

map(option(), (... -> any())) :: option()

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}
Link to this function

map_or(arg, default, fun)

Specs

map_or(option(), any(), (... -> any())) :: any()

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

Specs

none() :: option()

No value.

Specs

none?(option()) :: boolean()

Returns true if the option is a none value.

Examples

iex> ExOption.some(2) |> ExOption.none?()
false

iex> ExOption.none() |> ExOption.none?()
true
Link to this function

or_option(arg1, arg2)

Specs

or_option(option(), option()) :: option()

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}
Link to this function

replace(arg, value)

Specs

replace(option(), any()) :: option()

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}

Specs

some(any()) :: option()

Some value.

Specs

some?(option()) :: boolean()

Returns true if the option is a some value.

Examples

iex> ExOption.some(2) |> ExOption.some?()
true

iex> ExOption.none() |> ExOption.some?()
false

Specs

unwrap(option()) :: any()

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
Link to this function

unwrap_or(arg, default)

Specs

unwrap_or(option(), any()) :: any()

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"
Link to this function

xor_option(arg1, arg2)

Specs

xor_option(option(), option()) :: option()

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}
Link to this function

zip(arg1, arg2)

Specs

zip(option(), option()) :: option()

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}