Current section

Files

Jump to
gleam_stdlib gen src gleam@option.erl
Raw

gen/src/gleam@option.erl

-module(gleam@option).
-compile(no_auto_import).
-export([is_some/1, is_none/1, to_result/2, from_result/1, unwrap/2, map/2, flatten/1, then/2, 'or'/2]).
-export_type([option/1]).
-type option(IU) :: {some, IU} | none.
-spec is_some(option(any())) -> boolean().
is_some(Option) ->
Option /= none.
-spec is_none(option(any())) -> boolean().
is_none(Option) ->
Option =:= none.
-spec to_result(option(IZ), JC) -> {ok, IZ} | {error, JC}.
to_result(Option, E) ->
case Option of
{some, A} ->
{ok, A};
_ ->
{error, E}
end.
-spec from_result({ok, JF} | {error, any()}) -> option(JF).
from_result(Result) ->
case Result of
{ok, A} ->
{some, A};
_ ->
none
end.
-spec unwrap(option(JK), JK) -> JK.
unwrap(Option, Default) ->
case Option of
{some, X} ->
X;
none ->
Default
end.
-spec map(option(JM), fun((JM) -> JO)) -> option(JO).
map(Option, Fun) ->
case Option of
{some, X} ->
{some, Fun(X)};
none ->
none
end.
-spec flatten(option(option(JQ))) -> option(JQ).
flatten(Option) ->
case Option of
{some, X} ->
X;
none ->
none
end.
-spec then(option(JU), fun((JU) -> option(JW))) -> option(JW).
then(Option, Fun) ->
case Option of
{some, X} ->
Fun(X);
none ->
none
end.
-spec 'or'(option(JZ), option(JZ)) -> option(JZ).
'or'(First, Second) ->
case First of
{some, _} ->
First;
none ->
Second
end.