Package: effect
Module: Optic
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
Optional<S, ReadonlyArray<A>> — the focused value is an
array of all matched elements..forEach() to add per-element sub-optics (filtering, drilling
deeper)..modifyAll(f) to map a function over every focused element.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]
See
getAll — extract focused elementsOptional — the base typeSignature
export interface Traversal<in out S, in out A> extends Optional<S, ReadonlyArray<A>> {}
Since v4.0.0