Skip to content

Optional

Optional: object

Defined in: Core/Optional.ts:24

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.

A

B

Optional<A, B>

<S>(outer) => Optional<S, B>

const deepOpt = pipe(
  Optional.from.property<User>()("address"),
  Optional.andThen(Optional.from.property<Address>()("landmark")),
);

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.

A

B

Lens<A, B>

<S>(outer) => Optional<S, B>

const cityOpt = pipe(
  Optional.from.property<User>()("address"),
  Optional.andThenLens(Lens.from.property<Address>()("city")),
);

fold: <S, A>(opt) => <B>(onNone, onSome) => (s) => B

Extracts a value from an Optional focus using handlers for the present and absent cases.

S

A

Optional<S, A>

<B>(onNone, onSome) => (s) => B

pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));

from: object

accessors: <S, A>(get, set) => Optional<S, A> = makeAccessors

Constructs an Optional from a getter (returning Maybe) and a setter.

S

A

(s) => Maybe<A>

(a) => (s) => S

Optional<S, A>

const firstChar = Optional.from.accessors(
  (s: string) => s.length > 0 ? Maybe.make.some(s[0]) : Maybe.make.none(),
  (c) => (s) => s.length > 0 ? c + s.slice(1) : s,
);

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.

S

<K>(key) => Optional<S, NonNullable<S[K]>>

type Profile = { username: string; bio?: string };
const bioOpt = Optional.from.property<Profile>()("bio");

get: <S, A>(opt) => (s) => Maybe<A>

Reads the focused value from a structure, returning Maybe.

S

A

Optional<S, A>

(s) => Maybe<A>

pipe(profile, Optional.get(bioOpt)); // Some("...") or None

getOrElse: <S, A>(opt) => (defaultValue) => (s) => A

Returns the focused value or a default when the focus is absent.

S

A

Optional<S, A>

(defaultValue) => (s) => A

pipe(profile, Optional.getOrElse(bioOpt)(() => "no bio"));

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.

A

number

Optional<A[], A>

const firstItem = Optional.index<string>(0);

pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
pipe([], Optional.get(firstItem));         // None

match: <S, A>(opt) => <B>(cases) => (s) => B

Pattern matches on an Optional focus using a named-case object.

S

A

Optional<S, A>

<B>(cases) => (s) => B

pipe(
  profile,
  Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
);

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.

S

A

Optional<S, A>

(f) => (s) => S

pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));

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.

S

A

Optional<S, A>

(a) => (s) => S

pipe(profile, Optional.set(bioOpt)("hello"));