bubblewrap v0.4.3 Bubblewrap.Option
Option module provides Option type with utility functions.
Link to this section Summary
Functions
Applies function that returns a boolean using the value of the monadic type. If false, the value will be set to nil
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
t(a)
t(a) :: a | nil
t(a) :: a | nil
Option type.
Link to this section Functions
filter(a, f)
Applies function that returns a boolean using the value of the monadic type. If false, the value will be set to nil.
Example:
f = fn (x) ->
x == 0
end
5 |> filter(f) == 5
0 |> filter(f) == nil
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
foreach(z, f)
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
get(x)
Returns content of option if argument is not nil, raises otherwise
Examples
iex> 5 |> get
5
is_none(arg1)
Returns true if argument is nil
Examples
iex> is_none(nil)
true
iex> is_none(5)
false
is_some(arg1)
Returns true if argument is not nil
Examples
iex> is_some(5)
true
iex> is_some(nil)
false
map(x, f)
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
ok_or_else(x, f)
ok_or_else(t(a), err | (() -> err)) :: Bubblewrap.Result.t(a, err)
when a: any(), err: any()
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}
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