import * as optio from "https://deno.land/x/optio@1.0.0/mod.ts";
Optional values.
Type Option
represents an optional value: every Option
is either Some
and contains a value, or None
, and does not.
Operators overview
Option
provides a wide variety of different operators.
Querying the variant
The isSome
and isNone
return true
if the Option
is Some
or None
,
respectively.
Extracting the contained value
Extract the contained value in an Option<T>
when it is the Some
.
expect
throws with a provided custom messageunwrap
throws with generic messageunwrapOr
returns the provided default valueunwrapOrElse
returns the result of evaluating the provided function
Transforming contained values
Transform the Some
:
filter
calls the provided predicate function on the contained valueT
if theOption
isSome<T>
, and returnsSome<T>
if the function returnstrue
; otherwise, returnsNone
flat
removes one level of nesting from anOption<Option<T>>
map
transformsOption<T>
toOption<U>
by applying the provided function to the contained value ofSome
and leavingNone
values unchanged
Transform Option<T>
to a value of a possibly different type U
:
mapOr
applies the provided function to the contained value ofSome
, or returns the provided default value if theOption
isNone
mapOrElse
applies the provided function to the contained value ofSome
, or returns the result of evaluating the provided fallback function if theOption
isNone
Combine the Some
of two Option
values:
zip
returnsSome<[T,
| U]> ifoption
isSome<T>
and the providedOption
value isSome<U>
; otherwise, returnsNone
Logical operators
Treat the Option
as a boolean value, where Some
acts like true
and None
acts like false
.
The and
, or
, and xor
take another Option
, and produce an Option
as output.
Only the and
can produce an Option<U>
value having a different inner type U
than Option<T>
.
name | option | input | output |
---|---|---|---|
and |
None |
- | None |
and |
Some<T> |
None |
None |
and |
Some<T> |
Some<U> |
Some<U> |
or |
None |
None |
None |
or |
None |
Some<U> |
Some<U> |
or |
Some<T> |
- | Some<T> |
xor |
None |
None |
None |
xor |
None |
Some<U> |
Some<U> |
xor |
Some<T> |
None |
Some<T> |
xor |
Some<T> |
Some<U> |
None |
The andThen
and orElse
take a function as input, and
only evaluate the function when they need to produce a new value.
Only the andThen
can produce an Option<U>
value having a different inner type U
than Option<T>
.
name | option | function input | function result | output |
---|---|---|---|---|
andThen |
None |
- | - | None |
andThen |
Some<T> |
T |
None |
None |
andThen |
Some<T> |
T |
Some<U> |
Some<U> |
orElse |
None |
- | None |
None |
orElse |
None |
- | Some<U> |
Some<U> |
orElse |
Some<T> |
- | - | Some<T> |
Enums
The option type. |
Functions
f and | |
Returns the contained | |
f flat | Converts from |
Returns | |
Returns | |
f map | |
Pattern matching for | |
f or | Returns the |
Returns the | |
Returns the contained | |
Returns the contained | |
Returns the contained | |
f xor | |
f zip | Zips |