Module Relude_Option_Base
let isNone: a. option('a) => bool;
Option.isNone
returnstrue
if the provided option does not containSome
value.
let fold: a b. 'b => ('a => 'b) => option('a) => 'b;
Option.fold
applies the provided function to the inner value when the given option isSome
, or returns the provided default value if the option isNone
.This is not a lazy function, as the
default
value is always constructed, whether or not that value is needed. SeefoldLazy
for an alternative.Option.fold(0.0, x => 1.0 /. x, Some(2.0)) == 0.5; Option.fold(0.0, x => 1.0 /. x, None) == 0.0;
let foldLazy: a b. (unit => 'b) => ('a => 'b) => option('a) => 'b;
Option.foldLazy
applies the provided mapping function to the inner value in the option if the option isSome
, otherwise a default value is computed and returned by calling the provided fallback function.This is a lazy function because the default value is not evaluated unless the option is
None
. This is useful when the fallback value is expensive to compute.Option.foldLazy(() => "empty", Int.toString, Some(2)) == "2"; Option.foldLazy(() => "empty", Int.toString, None) == "empty";
let getOrElse: a. 'a => option('a) => 'a;
getOrElse(default, opt)
returnsv
whenopt
isSome(v)
. Ifopt
isNone
,getOrelse
returnsdefault
.If the option is
None
,getOrElse
returnsdefault
, which must be of the same type that asv
.This is not a lazy function, as the
default
value is always evaluated, no matter whatopt
’s value is.getOrElse(0, Some(3)) == 3; getOrElse(0, None) == 0;
let getOrElseLazy: a. (unit => 'a) => option('a) => 'a;
Option.getOrElseLazy
returns the inner value when the provided option isSome
. If the option isNone
, the fallback function is called, and its value is returned. This fallback takes no parameters and returns a value of the same type as the inner option.This is a lazy function, meaning the default value is not computed unless the option is
None
. This is useful in cases where the default is expensive to construct.Option.getOrElseLazy(() => 0, Some(3)) == 3; Option.getOrElseLazy(() => 0, None) == 0;
let getOrThrow: a. option('a) => 'a;
getOrThrow(opt)
returns the value of the option or throws an exception.This should only be used if you are absolutely sure there is a value in the option.
let orElse: a. fallback:option('a) => option('a) => option('a);
Similar to alt, but with the arguments reversed and labelled for use with
|>
let orElseLazy: a. fallback:(unit => option('a)) => option('a) => option('a);
Similar to alt, but with the arguments reversed and labelled for use with
|>
. The fallback value is also lazy for expensive constructions.
let tap: a. ('a => unit) => option('a) => option('a);
tap(f, opt)
applies a side-effect function to the value in aSome
, and returns the original option value untouched.
let tapSome: a. ('a => unit) => option('a) => option('a);
tapSome
is an alias fortap
.
let tapNone: a. (unit => unit) => option('a) => option('a);
tap(f, opt)
applies a side-effect function if the value of the option is None, and returns the original option value untouched.
let bitap: a. (unit => unit) => ('a => unit) => option('a) => option('a);
bitap(ifNone, ifSome, opt)
applies a side effect function for each of the cases of the option, and returns the original option untouched.
let filter: a. ('a => bool) => option('a) => option('a);
filter(f, opt)
works as follows:- If
opt
isSome(v)
andf(v)
istrue
, the result isSome(v)
. - If
opt
isSome(v)
andf(v)
isfalse
, the result isNone
. - If
opt
isNone
, the result isNone
.
let isEven = x => x mod 2 == 0; filter(isEven, Some(2)) == Some(2); filter(isEven, Some(3)) == None; filter(isEven, None) == None;
- If
let keep: a. ('a => bool) => option('a) => option('a);
Option.keep
is an alias forfilter
.