Hyperlinkv0.8.0-beta.28

Optic

Optic.Traversalinterfaceeffect/Optic.ts:1033
Traversal<S, A>

An optic that focuses on zero or more elements of type A inside S.

When to use

Use when you want to read/update multiple elements at once (e.g. all items in an array, or a filtered subset).

Details

  • Technically Optional<S, ReadonlyArray<A>> — the focused value is an array of all matched elements.
  • Use .forEach() to add per-element sub-optics (filtering, drilling deeper).
  • Use .modifyAll(f) to map a function over every focused element.
  • Use getAll to extract all focused elements as a plain array.

Example (Traversing array elements with a filter)

import { Optic, Schema } from "effect"

type S = { readonly items: ReadonlyArray<number> }

const _positive = Optic.id<S>()
  .key("items")
  .forEach((n) => n.check(Schema.isGreaterThan(0)))

const getPositive = Optic.getAll(_positive)

console.log(getPositive({ items: [1, -2, 3] }))
// Output: [1, 3]
Source effect/Optic.ts:10331 lines
export interface Traversal<in out S, in out A> extends Optional<S, ReadonlyArray<A>> {}
Referenced by 2 symbols