You are currently looking at the < v8.2.0 docs (Reason v3.6 syntax edition). You can find the latest API docs here.
(These docs cover all versions between v3 to v8 and are equivalent to the old BuckleScript docs before the rebrand)
Option
Provide utilities for handling option
.
t
REtype t('a) = option('a);
some
RElet some: 'a => option('a);
Wraps the given value in Some()
REJs.Option.some(1066) == Some(1066);
isSome
RElet isSome: option('a) => bool;
Returns true
if the argument is Some(value)
; false
if the argument is None
.
isSomeValue
RElet isSomeValue: ((. 'a, 'a) => bool, 'a, option('a)) => bool;
The first argument to isSomeValue
is an uncurried function eq()
that takes two arguments and returns true
if they are considered to be equal. It is used to compare a plain value v1
(the second argument) with an option
value. If the option
value is None
, isSomeValue()
returns false
; if the third argument is Some(v2)
, isSomeValue()
returns the result of calling eq(v1, v2)
.
RElet clockEqual = (. a, b) => (a mod 12 == b mod 12);
Js.Option.isSomeValue(clockEqual, 3, Some(15)) == true;
Js.Option.isSomeValue(clockEqual, 3, Some(4)) == false;
Js.Option.isSomeValue(clockEqual, 3, None) == false;
isNone
RElet isNone: option('a) => bool;
Returns true
if the argument is None
; false
otherwise.
getExn
RElet getExn: option('a) => 'a;
If the argument to getExn()
is of the form Some(value)
, returns value
. If given None
, it throws a getExn
exception.
equal
RElet equal: ((. 'a, 'b) => bool, option('a), option('b)) => bool;
The first argument to equal
is an uncurried function eq()
that takes two arguments and returns true
if they are considered to be equal. The second and third arguments are option
values.
If the second and third arguments are of the form:
Some(v1)
andSome(v2)
: returnseq(v1, v2)
Some(v1)
andNone
: returnsfalse
None
andSome(v2)
: returnsfalse
None
andNone
: returnstrue
RElet clockEqual = (. a, b) => (a mod 12 == b mod 12);
Js.Option.equal(clockEqual, Some(3), Some(15)) == true;
Js.Option.equal(clockEqual, Some(3), Some(16)) == false;
Js.Option.equal(clockEqual, Some(3), None) == false;
Js.Option.equal(clockEqual, None, Some(15)) == false;
Js.Option.equal(clockEqual, None, None) == true;
andThen
RElet andThen: ((. 'a) => option('b), option('a)) => option('b);
The first argument to andThen()
is an uncurried function f()
that takes a plain value and returns an option
result. The second argument is an option
value. If the second argument is None
, the return value is None
. If the second argument is Some(v)
, the return value is f(v)
.
RElet reciprocal = (. x) => (x == 0) ? None : Some(1.0 /. (float_of_int(x)));
Js.Option.andThen(reciprocal, Some(5)) == Some(0.2);
Js.Option.andThen(reciprocal, Some(0)) == None;
Js.Option.andThen(reciprocal, None) == None;
map
RElet map: ((. 'a) => 'b, option('a)) => option('b);
The first argument to map()
is an uncurried function f()
that takes a plain value and returns a plain result. The second argument is an option
value. If it is of the form Some(v)
, map()
returns Some(f(v))
; if it is None
, the return value is None
, and function f()
is not called.
RElet square = (. x) => (x * x);
Js.Option.map(square, Some(3)) == Some(9);
Js.Option.map(square, None) == None;
getWithDefault
RElet getWithDefault: ('a, option('a)) => 'a;
The first argument to getWithDefault()
is a default value. If the second argument is of the form Some(v)
, getWithDefault()
returns v
; if the second argument is None
, the return value is the default value.
REJs.Option.getWithDefault(1066, Some(15)) == 15;
Js.Option.getWithDefault(1066, None) == 1066;
default
RElet default: ('a, option('a)) => 'a;
See: getWithDefault
filter
RElet filter: ((. 'a) => bool, option('a)) => option('a);
The first argument to filter()
is an uncurried function that takes a plain value and returns a boolean. The second argument is an option
value.
If the second argument is of the form Some(v)
and f(v)
is true
,
the return value is Some(v)
. Otherwise, the return value is None
.
RElet isEven = (. x) => {x mod 2 == 0};
Js.Option.filter(isEven, Some(2)) == Some(2);
Js.Option.filter(isEven, Some(3)) == None;
Js.Option.filter(isEven, None) == None;
firstSome
RElet firstSome: (option('a), option('a)) => option('a);
The firstSome()
function takes two option
values; if the first is of the form Some(v1)
, that is the return value. Otherwise, firstSome()
returns the second value.
REJs.Option.firstSome(Some("one"), Some("two")) == Some("one");
Js.Option.firstSome(Some("one"), None) == Some("one");
Js.Option.firstSome(None, Some("two")) == Some("two");
Js.Option.firstSome(None, None) == None;