Optional
Optional:
object
Defined in: Core/Optional.ts:24
Type Declaration
Section titled “Type Declaration”andThen
Section titled “andThen”andThen: <
A,B>(inner) => <S>(outer) =>Optional<S,B>
Composes two Optionals: focuses through the outer, then through the inner. Returns None if either focus is absent.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”Optional<A, B>
Returns
Section titled “Returns”<S>(outer) => Optional<S, B>
Example
Section titled “Example”andThenLens
Section titled “andThenLens”andThenLens: <
A,B>(inner) => <S>(outer) =>Optional<S,B>
Composes an Optional with a Lens, producing an Optional. The Lens focuses within the value found by the Optional.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”Lens<A, B>
Returns
Section titled “Returns”<S>(outer) => Optional<S, B>
Example
Section titled “Example”fold: <
S,A>(opt) => <B>(onNone,onSome) => (s) =>B
Extracts a value from an Optional focus using handlers for the present and absent cases.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”<B>(onNone, onSome) => (s) => B
Example
Section titled “Example”from:
object
from.accessors
Section titled “from.accessors”accessors: <
S,A>(get,set) =>Optional<S,A> =makeAccessors
Constructs an Optional from a getter (returning Maybe) and a setter.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”(s) => Maybe<A>
(a) => (s) => S
Returns
Section titled “Returns”Optional<S, A>
Example
Section titled “Example”from.property
Section titled “from.property”property: <
S>() => <K>(key) =>Optional<S,NonNullable<S[K]>> =makeProperty
Creates an Optional that focuses on an optional property of an object.
Only keys whose type includes undefined (i.e. field?: T) are accepted.
Call with the structure type first, then the key.
Type Parameters
Section titled “Type Parameters”S
Returns
Section titled “Returns”<K>(key) => Optional<S, NonNullable<S[K]>>
Example
Section titled “Example”get: <
S,A>(opt) => (s) =>Maybe<A>
Reads the focused value from a structure, returning Maybe.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(s) => Maybe<A>
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
S,A>(opt) => (defaultValue) => (s) =>A
Returns the focused value or a default when the focus is absent.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(defaultValue) => (s) => A
Example
Section titled “Example”index: <
A>(i) =>Optional<A[],A>
Creates an Optional that focuses on an element at a given index in an array. Returns None when the index is out of bounds; set is a no-op when out of bounds.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”Optional<A[], A>
Example
Section titled “Example”match: <
S,A>(opt) => <B>(cases) => (s) =>B
Pattern matches on an Optional focus using a named-case object.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”<B>(cases) => (s) => B
Example
Section titled “Example”modify
Section titled “modify”modify: <
S,A>(opt) => (f) => (s) =>S
Applies a function to the focused value if it is present; returns the structure unchanged if the focus is absent.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(f) => (s) => S
Example
Section titled “Example”set: <
S,A>(opt) => (a) => (s) =>S
Replaces the focused value within a structure. For indexed focuses, this is a no-op when the index is out of bounds.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(a) => (s) => S