effect-io-ai

Package: effect
Module: Optic

Optic.Traversal

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

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]

See

Signature

export interface Traversal<in out S, in out A> extends Optional<S, ReadonlyArray<A>> {}

Source

Since v4.0.0