lkn-prelude v0.1.0 Lkn.Prelude.Option

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

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

Types

a()
a() :: any

The type of an optional value, before a computation.

b()
b() :: any

The type of an optional value, after a computation.

nothing()

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

some(x)
some(x)

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

t(x)
t(x) :: some(x) | nothing

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

Functions

inside(opt, x, list) (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.

map(arg, f)
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)
nothing() (macro)
nothing(term) :: nothing

A macro to create and match a nothing value.

some(x) (macro)
some(term, a) :: some(a)

A macro to create and match an optional value.

unwrap(x, default)
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
unwrap!(arg)
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