lkn-prelude v0.1.2 Lkn.Prelude.Option View Source

An optional value.

Whenever a function may or may not returns a value, the Option macros should be used. In a similar manner, these macros should be used in pattern matching blocks. This way, the inner representation can change without the refactoring pain.

The best way to use this module is by calling use Lkn.Prelude. This call aliases and requires each components of the lkn Prelude, including this module.

Example

use Lkn.Prelude

x = Option.some(3)
y = Option.nothing()

Option.unwrap(x, 1)
#=> 3
Option.unwrap!(x)
#=> 3
Option.unwrap(y, 1)
#=> 1

Link to this section Summary

Types

a()

The type of an optional value, before a computation

b()

The type of an optional value, after a computation

The result of a computation which could have returned something but didn’t

The result of a computation which could have failed to return something but didn’t

The result of a computation which could have failed or could have returned something

Functions

A macro to execute a block only if the given optional value exists

Apply a function to an optional value if it exists

A macro to create and match a nothing value

A macro to create and match an optional value

Get the value contained within a Lkn.Prelude.Option or a default value

Same as Lkn.Prelude.Option.unwrap/2, but panicked if there is no value to unwrap

Link to this section Types

The type of an optional value, before a computation.

The type of an optional value, after a computation.

Link to this type nothing() View Source
nothing() :: :nothing

The result of a computation which could have returned something but didn’t.

Link to this type some(x) View Source
some(x) :: {:some, x}

The result of a computation which could have failed to return something but didn’t.

The result of a computation which could have failed or could have returned something.

Link to this section Functions

Link to this macro inside(opt, x, list) View Source (macro)

A macro to execute a block only if the given optional value exists.

In expression, one can use x: it gets the value encapsulated in opt.

Link to this function map(arg, f) View Source
map(t(a), (a -> b)) :: t(b)

Apply a function to an optional value if it exists.

iex> Lkn.Prelude.Option.map(Lkn.Prelude.Option.some(3), &(&1 + 1))
Lkn.Prelude.Option.some(4)
Link to this macro nothing() View Source (macro)
nothing(term) :: nothing

A macro to create and match a nothing value.

Link to this macro some(x) View Source (macro)
some(term, a) :: some(a)

A macro to create and match an optional value.

Link to this function unwrap(x, default) View Source
unwrap(t(a), a) :: a

Get the value contained within a Lkn.Prelude.Option or a default value.

iex> Lkn.Prelude.Option.unwrap(Lkn.Prelude.Option.some(3), 1)
3
iex> Lkn.Prelude.Option.unwrap(Lkn.Prelude.Option.nothing(), 1)
1
Link to this function unwrap!(arg) View Source
unwrap!(some(a)) :: a

Same as Lkn.Prelude.Option.unwrap/2, but panicked if there is no value to unwrap.

iex> Lkn.Prelude.Option.unwrap!(Lkn.Prelude.Option.some(3))
3
iex> try do
...>   Lkn.Prelude.Option.unwrap!(Lkn.Prelude.Option.nothing())
...> rescue
...>   _ -> :ok
...> end
:ok