Option
We represent the existence and nonexistence of a value by wrapping it with
the option
type. In order to make it a bit more convenient to work with
option-types, we provide utility-functions for it.
The option
type is a part of the ReScript standard library which is defined
like this:
RESCRIPTtype option<'a> = None | Some('a)
RESCRIPTlet someString: option<string> = Some("hello")
t
type t<'a> = option<'a> = None | Some('a)
Type representing an option of type 'a.
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
Deprecated
let getExn: (option<'a>, ~message: string=?) => 'a
getExn(opt, ~message=?)
returns value
if opt
is Some(value)
, otherwise throws an exception with the message provided, or a generic message if no message was provided.
RESCRIPTOption.getExn(Some(3)) == 3
switch Option.getExn(None) {
| exception _ => assert(true)
| _ => assert(false)
}
switch Option.getExn(None, ~message="was None!") {
| exception _ => assert(true) // Throws a JsError with the message "was None!"
| _ => assert(false)
}
Exceptions
Throws an error if
opt
isNone
getOrThrow
let getOrThrow: (option<'a>, ~message: string=?) => 'a
getOrThrow(opt, ~message=?)
returns value
if opt
is Some(value)
, otherwise throws an exception with the message provided, or a generic message if no message was provided.
RESCRIPTOption.getOrThrow(Some(3)) == 3
switch Option.getOrThrow(None) {
| exception _ => assert(true)
| _ => assert(false)
}
switch Option.getOrThrow(None, ~message="was None!") {
| exception _ => assert(true) // Throws a JsError with the message "was None!"
| _ => assert(false)
}
Exceptions
Throws 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
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
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) => Ordering.t,
) => 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) => Int.compare(mod(a, 12), mod(b, 12))
Option.compare(Some(3), Some(15), clockCompare) // 0.
Option.compare(Some(3), Some(14), clockCompare) // 1.
Option.compare(Some(2), Some(15), clockCompare) // (-1.)
Option.compare(None, Some(15), clockCompare) // (-1.)
Option.compare(Some(14), None, clockCompare) // 1.
Option.compare(None, None, clockCompare) // 0.
all
let all: array<option<'a>> => option<array<'a>>
all(options)
returns an option of array if all options are Some, otherwise returns None.
Examples
RESCRIPTOption.all([Some(1), Some(2), Some(3)]) // Some([1, 2, 3])
Option.all([Some(1), None]) // None
all2
let all2: ((option<'a>, option<'b>)) => option<('a, 'b)>
all2((o1, o2))
. Like all()
, but with a fixed size tuple of 2
all3
let all3: (
(option<'a>, option<'b>, option<'c>),
) => option<('a, 'b, 'c)>
all3((o1, o2, o3))
. Like all()
, but with a fixed size tuple of 3
all4
let all4: (
(option<'a>, option<'b>, option<'c>, option<'d>),
) => option<('a, 'b, 'c, 'd)>
all4((o1, o2, o3, o4))
. Like all()
, but with a fixed size tuple of 4
all5
let all5: (
(
option<'a>,
option<'b>,
option<'c>,
option<'d>,
option<'e>,
),
) => option<('a, 'b, 'c, 'd, 'e)>
all5((o1, o2, o3, o4, o5))
. Like all()
, but with a fixed size tuple of 5
all6
let all6: (
(
option<'a>,
option<'b>,
option<'c>,
option<'d>,
option<'e>,
option<'f>,
),
) => option<('a, 'b, 'c, 'd, 'e, 'f)>
all6((o1, o2, o3, o4, o5, o6))
. Like all()
, but with a fixed size tuple of 6
ignore
let ignore: option<'a> => unit
ignore(option)
ignores the provided option and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.