Skip to content

Lens

Lens: object

Defined in: Core/Lens.ts:31

Utilities for reading, overwriting, and transforming values within data structures via composable bidirectional lenses.

andThen: <A, B>(inner) => <S>(outer) => Lens<S, B>

Composes two lenses to focus sequentially through nested structures.

A

B

Lens<A, B>

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

Lens.andThenOptional to compose a Lens with an Optional path.

type Address = { city: string };
type Customer = { address: Address };

const customerCityLens = pipe(
  Lens.from.property<Customer>()("address"),
  Lens.andThen(Lens.from.property<Address>()("city")),
);

andThenOptional: <A, B>(inner) => <S>(outer) => Optional<S, B>

Composes a Lens with an Optional, producing an Optional. Use when the next segment in the path may be absent.

A

B

Optional<A, B>

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

Lens.andThen to compose two guaranteed Lens paths.

type Profile = { bio?: string };
type Account = { profile: Profile };

const bioOptional = pipe(
  Lens.from.property<Account>()("profile"),
  Lens.andThenOptional(Optional.from.property<Profile>()("bio")),
);

from: object

Constructors for creating Lens instances from accessors or property keys.

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

Constructs a Lens from getter and setter functions.

S

A

(structure) => A

(value) => (structure) => S

Lens<S, A>

Lens.from.property to create a Lens focused on an object property.

type Coordinate = readonly [latitude: number, longitude: number];

const latitudeLens = Lens.from.accessors(
  (coord: Coordinate) => coord[0],
  (latitude) => (coord) => [latitude, coord[1]] as const,
);

property: <S>() => <K>(key) => Lens<S, S[K]> = makeProperty

Creates a Lens that focuses on a property of an object. Call with the structure type first, then the key.

S

<K>(key) => Lens<S, S[K]>

Lens.from.accessors to construct a Lens from custom getter and setter functions.

type Account = { id: string; email: string };
const emailLens = Lens.from.property<Account>()("email");

get: <S, A>(lens) => (structure) => A

Reads the focused value from a structure.

S

A

Lens<S, A>

(structure) => A

  • Lens.set to replace the focused value.
  • Lens.modify to transform the focused value with a function.
type Account = { email: string };
const emailLens = Lens.from.property<Account>()("email");
const account: Account = { email: "dev@example.com" };

pipe(account, Lens.get(emailLens)); // "dev@example.com"

modify: <S, A>(lens) => (transform) => (structure) => S

Applies a function to the focused value, returning a new structure. Preserves the original structure reference if the transformed value is identical.

S

A

Lens<S, A>

(transform) => (structure) => S

  • Lens.get to extract the focused value.
  • Lens.set to overwrite the focused value directly.
type Account = { email: string };
const emailLens = Lens.from.property<Account>()("email");
const account: Account = { email: "dev@example.com" };

pipe(account, Lens.modify(emailLens)((email) => email.toUpperCase()));
// { email: "DEV@EXAMPLE.COM" }

set: <S, A>(lens) => (value) => (structure) => S

Replaces the focused value within a structure, returning a new structure.

S

A

Lens<S, A>

(value) => (structure) => S

  • Lens.get to extract the focused value.
  • Lens.modify to transform the focused value with a function.
type Account = { email: string };
const emailLens = Lens.from.property<Account>()("email");
const account: Account = { email: "dev@example.com" };

pipe(account, Lens.set(emailLens)("ops@example.com"));
// { email: "ops@example.com" }

to: object

Converters from Lens to wider optic representations.

Optional: <S, A>(lens) => Optional<S, A>

Converts a Lens to an Optional whose getter always yields Some.

S

A

Lens<S, A>

Optional<S, A>

Lens.andThenOptional to compose directly with an Optional without manual conversion.

type Address = { street: string };
type User = { address: Address };

const addressOptional = Lens.to.Optional(Lens.from.property<User>()("address"));