bubblewrap v0.4.2 Bubblewrap.Option

Option module provides Option type with utility functions.

Link to this section Summary

Types

Option type

Functions

Applies function that returns monadic type itself to the content of the monadic type. This is useful in a chain of operations, where argument to the next op has to be unwrapped to proceed

Performs a calculation with the content of monadic container and returns the argument intact. Even though the convention says to return nothing (Unit) this one passes value along for convenience — this way we can perform more than one operation

Returns content of option if argument is not nil, raises otherwise

Returns true if argument is nil

Returns true if argument is not nil

Transforms the content of monadic type. Function is applied only if it's not nil. Otherwise value stays intact

Converts an Option into Result if value is present, otherwise returns second argument wrapped in error()

Returns option if argument is not nil, second argument which has to be option otherwise. Executes function, if it's supplied

Link to this section Types

Link to this type

t(a)
t(a) :: a | nil

Option type.

Link to this section Functions

Link to this function

flat_map(x, f)
flat_map(t(a), (a -> t(b))) :: t(b) when a: any(), b: any()

Applies function that returns monadic type itself to the content of the monadic type. This is useful in a chain of operations, where argument to the next op has to be unwrapped to proceed.

Example:

f = fn (x) ->
  if x == 0 do
    nil
  else
    1/x
  end
5 |> flat_map(f) == 1/5
0 |> flat_map(f) == nil
Link to this function

foreach(z, f)
foreach(t(a), (a -> no_return())) :: t(a) when a: any()

Performs a calculation with the content of monadic container and returns the argument intact. Even though the convention says to return nothing (Unit) this one passes value along for convenience — this way we can perform more than one operation.

5
|> foreach(fn x -> IO.inspect(x) end)
|> foreach(fn x -> IO.inspect(2 * x) end)

This will print: 5 10

Link to this function

get(x)
get(t(a)) :: a when a: any()

Returns content of option if argument is not nil, raises otherwise

Examples

iex> 5 |> get
5
Link to this function

is_none(arg1)
is_none(t(any())) :: boolean()

Returns true if argument is nil

Examples

iex> is_none(nil)
true

iex> is_none(5)
false
Link to this function

is_some(arg1)
is_some(t(any())) :: boolean()

Returns true if argument is not nil

Examples

iex> is_some(5)
true

iex> is_some(nil)
false
Link to this function

map(x, f)
map(t(a), (a -> b)) :: t(b) when a: any(), b: any()

Transforms the content of monadic type. Function is applied only if it's not nil. Otherwise value stays intact.

Example:

f = fn (x) ->
  x * 2
end
5 |> map(f) == 10
nil |> map(f) == nil
Link to this function

ok_or_else(x, f)
ok_or_else(t(a), err | (() -> err)) :: Bubblewrap.Result.t(a, err)
when a: any(), err: any()

Converts an Option into Result if value is present, otherwise returns second argument wrapped in error().

Examples

iex> 5 |> ok_or_else(2)
{:ok, 5}

...> nil |> ok_or_else(:missing_value)
{:error, :missing_value}

...> nil |> ok_or_else(fn -> :oh_no end)
{:error, :oh_no}
Link to this function

or_else(x, f)
or_else(t(a), t(a) | (() -> t(a))) :: t(a) when a: any()

Returns option if argument is not nil, second argument which has to be option otherwise. Executes function, if it's supplied.

Examples

iex> 5 |> or_else(2)
5

iex> nil |> or_else(2)
2

iex> nil |> or_else(fn -> 1 end)
1