Returns true if this option is nonempty and the value it
holds is equal to the given elem
.
Implements IEquals.equals.
is the right hand side of the equality check
Returns true
if this option is nonempty and the given
predicate returns true
when applied on this option's value.
is the predicate function to test
Returns this option if it is nonempty AND applying the
predicate p
to the underlying value yields true
,
otherwise return an empty option.
is the predicate function that is used to apply filtering on the option's value
a new option instance containing the value of the source filtered with the given predicate
Returns the result of applying f
to this option's value if
the option is nonempty, otherwise returns an empty option.
NOTE: this is similar with map
, except that flatMap
the
result returned by f
is expected to be boxed in an Option
already.
Example:
const opt = Option.of(10)
opt.flatMap(num => {
if (num % 2 == 0)
Some(num + 1)
else
None
})
the mapping function that will transform the value of this option if nonempty.
a new option instance containing the value of the source mapped by the given function
Returns the result of applying f
to this option's value,
or in case the option is empty, the return the result of
evaluating the fallback
function.
This function is equivalent with:
opt.map(f).getOrElseL(fallback)
is the function to be evaluated in case this option is empty
is the mapping function for transforming this option's value in case it is nonempty
Returns true if this option is empty or the given predicate
returns true
when applied on this option's value.
is the predicate function to test
Apply the given procedure cb
to the option's value if
this option is nonempty, otherwise do nothing.
the procedure to apply
Returns the option's value.
WARNING!
This function is partial, the option must be non-empty, otherwise a runtime exception will get thrown. Use with care.
Returns the option's value if the option is nonempty, otherwise
return the given fallback
.
See Option.getOrElseL for a lazy alternative.
Returns the option's value if the option is nonempty, otherwise
return the result of evaluating thunk
.
See Option.getOrElse for a strict alternative.
Returns true
if the option is empty, false
otherwise.
Returns an option containing the result of applying f
to
this option's value, or an empty option if the source is empty.
NOTE: this is similar with flatMap
, except with map
the
result of f
doesn't need to be wrapped in an Option
.
the mapping function that will transform the value of this option if nonempty.
a new option instance containing the value of the source mapped by the given function
Returns true
if the option is not empty, false
otherwise.
Returns the option's value if the option is nonempty, otherwise
return null
.
`
Returns the option's value if the option is nonempty, otherwise
return undefined
.
Returns an empty Option.
Similar to Option.none, but this one allows specifying a type parameter (in the context of TypeScript or Flow or other type system).
NOTE: Because Option
is immutable, this function returns the
same cached reference is on different calls.
Maps 2 optional values by the mapping function, returning a new
optional reference that is Some
only if both option values are
Some
, otherwise it returns a None
.
// Yields Some(3)
Option.map2(Some(1), Some(2),
(a, b) => a + b
)
// Yields None, because the second arg is None
Option.map2(Some(1), None,
(a, b) => a + b
)
This operation is the Applicative.map2
.
Maps 3 optional values by the mapping function, returning a new
optional reference that is Some
only if all 3 option values are
Some
, otherwise it returns a None
.
// Yields Some(6)
Option.map3(Some(1), Some(2), Some(3),
(a, b, c) => a + b + c
)
// Yields None, because the second arg is None
Option.map3(Some(1), None, Some(3),
(a, b, c) => a + b + c
)
Maps 4 optional values by the mapping function, returning a new
optional reference that is Some
only if all 4 option values are
Some
, otherwise it returns a None
.
// Yields Some(10)
Option.map4(Some(1), Some(2), Some(3), Some(4),
(a, b, c, d) => a + b + c + d
)
// Yields None, because the second arg is None
Option.map4(Some(1), None, Some(3), Some(4),
(a, b, c, d) => a + b + c + d
)
Maps 5 optional values by the mapping function, returning a new
optional reference that is Some
only if all 5 option values are
Some
, otherwise it returns a None
.
// Yields Some(15)
Option.map5(Some(1), Some(2), Some(3), Some(4), Some(5),
(a, b, c, d, e) => a + b + c + d + e
)
// Yields None, because the second arg is None
Option.map5(Some(1), None, Some(3), Some(4), Some(5),
(a, b, c, d, e) => a + b + c + d + e
)
Maps 6 optional values by the mapping function, returning a new
optional reference that is Some
only if all 6 option values are
Some
, otherwise it returns a None
.
// Yields Some(21)
Option.map6(Some(1), Some(2), Some(3), Some(4), Some(5), Some(6),
(a, b, c, d, e, f) => a + b + c + d + e + f
)
// Yields None, because the second arg is None
Option.map6(Some(1), None, Some(3), Some(4), Some(5), Some(6),
(a, b, c, d, e, f) => a + b + c + d + e + f
)
Builds an Option reference that contains the given reference.
Note that value
is allowed to be null
or undefined
, the
returned option will still be non-empty. Use Option.of
if you want to avoid this problem. This means:
const opt = Some<number | null>(null)
opt.isEmpty()
//=> false
opt.get()
//=> null
Keeps calling f
until a Right(b)
is returned.
Based on Phil Freeman's Stack Safety for Free.
Described in FlatMap.tailRecM
.
Generated using TypeDoc
Represents optional values, inspired by Scala's
Option
and by Haskell'sMaybe
data types.Option is an immutable data type, represented as a sum type, being either a Some, in case it contains a single element, or a None, in case it is empty.
The most idiomatic way to use an
Option
instance is to treat it as a collection or monad and usemap
,flatMap
,filter
, orforEach
.