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
The type of an optional value, before a computation
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.
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.
Link to this section Functions
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
.
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)
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.
iex> Lkn.Prelude.Option.unwrap(Lkn.Prelude.Option.some(3), 1)
3
iex> Lkn.Prelude.Option.unwrap(Lkn.Prelude.Option.nothing(), 1)
1
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