Option
filter
let filter: (option<'a>, 'a => bool) => option<'a>
filter(opt, f)
applies f
to opt
, if f
returns true
, then it returns Some(value)
, otherwise returns None
.
Examples
RESCRIPTOption.filter(Some(10), x => x > 5) // Some(10)
Option.filter(Some(4), x => x > 5) // None
Option.filter(None, x => x > 5) // None
forEach
let forEach: (option<'a>, 'a => unit) => unit
forEach(opt, f)
call f
on opt
. if opt
is Some(value)
, then if calls
f
, otherwise returns unit
.
Examples
RESCRIPTOption.forEach(Some("thing"), x => Console.log(x)) // logs "thing"
Option.forEach(None, x => Console.log(x)) // returns ()
getExn
let getExn: option<'a> => 'a
getExn(opt)
returns value
if opt
is Some(value)
, otherwise raises an exception.
RESCRIPTOption.getExn(Some(3)) // 3
Option.getExn(None) /* Raises an Error */
Exceptions
Raises an error if
opt
isNone
getUnsafe
let getUnsafe: option<'a> => 'a
getUnsafe(opt)
returns value
if opt
is Some(value)
, otherwise undefined
.
Examples
RESCRIPTOption.getUnsafe(Some(3)) == 3
Option.getUnsafe(None: option<int>) // Returns `undefined`, which is not a valid `int`
Notes
This is an unsafe operation. It assumes
value
is notNone
, and may cause undefined behaviour if it is.
mapOr
let mapOr: (option<'a>, 'b, 'a => 'b) => 'b
mapOr(opt, default, f)
returns f(value)
if opt
is Some(value)
, otherwise default
.
Examples
RESCRIPTlet someValue = Some(3)
someValue->Option.mapOr(0, x => x + 5) // 8
let noneValue = None
noneValue->Option.mapOr(0, x => x + 5) // 0
mapWithDefault
Deprecated
Use mapOr instead
let mapWithDefault: (option<'a>, 'b, 'a => 'b) => 'b
map
let map: (option<'a>, 'a => 'b) => option<'b>
map(opt, f)
returns Some(f(value))
if opt
is Some(value)
, otherwise None
.
Examples
RESCRIPTOption.map(Some(3), x => x * x) // Some(9)
Option.map(None, x => x * x) // None
flatMap
let flatMap: (option<'a>, 'a => option<'b>) => option<'b>
flatMap(opt, f)
returns f(value)
if opt
is Some(value)
, otherwise None
.
Examples
RESCRIPTlet addIfAboveOne = value =>
if (value > 1) {
Some(value + 1)
} else {
None
}
Option.flatMap(Some(2), addIfAboveOne) // Some(3)
Option.flatMap(Some(-4), addIfAboveOne) // None
Option.flatMap(None, addIfAboveOne) // None
getOr
let getOr: (option<'a>, 'a) => 'a
getOr(opt, default)
returns value
if opt
is Some(value)
, otherwise default
.
Examples
RESCRIPTOption.getOr(None, "Banana") // Banana
Option.getOr(Some("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Option.getOr("Anonymous")
Some("Jane")->greet // "Greetings Jane"
None->greet // "Greetings Anonymous"
getWithDefault
Deprecated
Use getOr instead
let getWithDefault: (option<'a>, 'a) => 'a
orElse
let orElse: (option<'a>, option<'a>) => option<'a>
orElse(opt1, opt2)
returns opt2
if opt1
is None
, otherwise opt1
.
Examples
RESCRIPTOption.orElse(Some(1812), Some(1066)) == Some(1812)
Option.orElse(None, Some(1066) == Some(1066)
Option.orElse(None, None) == None
isSome
let isSome: option<'a> => bool
isSome(opt)
returns true
if opt
is Some(value)
, otherwise returns false
.
Examples
RESCRIPTOption.isSome(None) // false
Option.isSome(Some(1)) // true
isNone
let isNone: option<'a> => bool
isNone(opt)
returns true
if opt
is None
, false otherwise.
Examples
RESCRIPTOption.isNone(None) // true
Option.isNone(Some(1)) // false
equal
let equal: (option<'a>, option<'b>, ('a, 'b) => bool) => bool
equal(opt1, opt2, f)
evaluates two optional values for equality with respect to a predicate function f
. If both opt1
and opt2
are None
, returns true
.
If one of the arguments is Some(value)
and the other is None
, returns
false
.
If arguments are Some(value1)
and Some(value2)
, returns the result of
f(value1, value2)
, the predicate function f
must return a bool.
Examples
RESCRIPTlet clockEqual = (a, b) => mod(a, 12) == mod(b, 12)
open Option
equal(Some(3), Some(15), clockEqual) // true
equal(Some(3), None, clockEqual) // false
equal(None, Some(3), clockEqual) // false
equal(None, None, clockEqual) // true
compare
let compare: (
option<'a>,
option<'b>,
('a, 'b) => Core__Ordering.t,
) => Core__Ordering.t
compare(opt1, opt2, f)
compares two optional values with respect to given f
.
If both opt1
and opt2
are None
, it returns 0.
. If the first argument is Some(value1)
and the second is None
, returns 1.
(something is greater than nothing).
If the first argument is None
and the second is Some(value2)
, returns -1.
(nothing is less than something).
If the arguments are Some(value1)
and Some(value2)
, returns the result of
f(value1, value2)
, f
takes two arguments and returns -1.
if the first
argument is less than the second, 0.
if the arguments are equal, and 1.
if
the first argument is greater than the second.
Examples
RESCRIPTlet clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12))
open Option
compare(Some(3), Some(15), clockCompare) // 0.
compare(Some(3), Some(14), clockCompare) // 1.
compare(Some(2), Some(15), clockCompare) // (-1.)
compare(None, Some(15), clockCompare) // (-1.)
compare(Some(14), None, clockCompare) // 1.
compare(None, None, clockCompare) // 0.