<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>Returns a function that extracts all elements focused by a Traversal as a plain mutable array.
When to use
Use when you need the focused values as a simple Array<A> for further
processing.
Details
- Returns an empty array when the traversal cannot focus.
- Always returns a fresh array (safe to mutate).
Example (Collecting positive numbers)
import { Optic, Schema } from "effect"
type S = { readonly values: ReadonlyArray<number> }
const _pos = Optic.id<S>()
.key("values")
.forEach((n) => n.check(Schema.isGreaterThan(0)))
const getPositive = Optic.getAll(_pos)
console.log(getPositive({ values: [3, -1, 5] }))
// Output: [3, 5]
console.log(getPositive({ values: [-1, -2] }))
// Output: []export function function getAll<S, A>(
traversal: Traversal<S, A>
): (s: S) => Array<A>
Returns a function that extracts all elements focused by a
Traversal
as a plain mutable array.
When to use
Use when you need the focused values as a simple Array<A> for further
processing.
Details
- Returns an empty array when the traversal cannot focus.
- Always returns a fresh array (safe to mutate).
Example (Collecting positive numbers)
import { Optic, Schema } from "effect"
type S = { readonly values: ReadonlyArray<number> }
const _pos = Optic.id<S>()
.key("values")
.forEach((n) => n.check(Schema.isGreaterThan(0)))
const getPositive = Optic.getAll(_pos)
console.log(getPositive({ values: [3, -1, 5] }))
// Output: [3, 5]
console.log(getPositive({ values: [-1, -2] }))
// Output: []
getAll<function (type parameter) S in getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>S, function (type parameter) A in getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>A>(traversal: Traversal<S, A>(parameter) traversal: {
node: Node;
getResult: (s: S) => Result.Result<A, string>;
replace: (a: A, s: S) => S;
replaceResult: (a: A, s: S) => Result.Result<S, string>;
compose: { (this: Iso<S, ReadonlyArray<A>>, that: Iso<ReadonlyArray<A>, B>): Iso<S, B>; (this: Lens<S, ReadonlyArray<A>>, that: Lens<ReadonlyArray<A>, B>): Lens<S, B>; (this: Prism<S, ReadonlyArray<A>>, that: Prism<ReadonlyArray<A>, B>): Prism<S, B…;
modify: (f: (a: ReadonlyArray<A>) => ReadonlyArray<A>) => (s: S) => S;
key: { <S, A extends object, Key extends keyof A>(this: Lens<S, A>, key: Key, ..._err: ForbidUnion<A, 'cannot use `key` on a union type'>): Lens<S, A[Key]>; <S, A extends object, Key extends keyof A>(this: Optional<S, A>, key: Key, ..._err: For…;
optionalKey: { <S, A extends object, Key extends keyof A>(this: Lens<S, A>, key: Key, ..._err: ForbidUnion<A, 'cannot use `optionalKey` on a union type'>): Lens<S, A[Key] | undefined>; <S, A extends object, Key extends keyof A>(this: Optional<S, A>, ke…;
check: { <S, A>(this: Prism<S, A>, checks_0: SchemaAST.Check<A>, ...checks: Array<SchemaAST.Check<A>>): Prism<S, A>; <S, A>(this: Optional<S, A>, checks_0: SchemaAST.Check<A>, ...checks: Array<SchemaAST.Check<A>>): Optional<S, A> };
refine: { <S, A, B extends A>(this: Prism<S, A>, refinement: (a: A) => a is B, annotations?: Schema.Annotations.Filter): Prism<S, B>; <S, A, B extends A>(this: Optional<S, A>, refinement: (a: A) => a is B, annotations?: Schema.Annotations.Filter):…;
tag: { <S, A extends { readonly _tag: SchemaAST.LiteralValue }, Tag extends A['_tag']>(this: Prism<S, A>, tag: Tag): Prism<S, Extract<A, { readonly _tag: Tag }>>; <S, A extends { readonly _tag: SchemaAST.LiteralValue }, Tag extends A['_tag']>(t…;
at: <S, A extends object, Key extends keyof A>(this: Optional<S, A>, key: Key, ..._err: ForbidUnion<A, 'cannot use `at` on a union type'>) => Optional<S, A[Key]>;
pick: { <S, A, Keys extends ReadonlyArray<keyof A>>(this: Lens<S, A>, keys: Keys, ..._err: ForbidUnion<A, 'cannot use `pick` on a union type'>): Lens<S, Pick<A, Keys[number]>>; <S, A, Keys extends ReadonlyArray<keyof A>>(this: Optional<S, A>, ke…;
omit: { <S, A, Keys extends ReadonlyArray<keyof A>>(this: Lens<S, A>, keys: Keys, ..._err: ForbidUnion<A, 'cannot use `omit` on a union type'>): Lens<S, Omit<A, Keys[number]>>; <S, A, Keys extends ReadonlyArray<keyof A>>(this: Optional<S, A>, ke…;
notUndefined: { (): Prism<S, ReadonlyArray<A>>; (): Optional<S, ReadonlyArray<A>> };
forEach: <S, A, B>(this: Traversal<S, A>, f: (iso: Iso<A, A>) => Optional<A, B>) => Traversal<S, B>;
modifyAll: <S, A>(this: Traversal<S, A>, f: (a: A) => A) => (s: S) => S;
}
traversal: interface Traversal<in out S, in out 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]
Traversal<function (type parameter) S in getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>S, function (type parameter) A in getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>A>): (s: Ss: function (type parameter) S in getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>S) => interface Array<T>Array<function (type parameter) A in getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A>A> {
return (s: Ss) =>
import ResultResult.const match: {
<E, B, A, C = B>(options: {
readonly onFailure: (error: E) => B
readonly onSuccess: (ok: A) => C
}): (self: Result<A, E>) => B | C
<A, E, B, C = B>(
self: Result<A, E>,
options: {
readonly onFailure: (error: E) => B
readonly onSuccess: (ok: A) => C
}
): B | C
}
match(traversal: Traversal<S, A>(parameter) traversal: {
node: Node;
getResult: (s: S) => Result.Result<A, string>;
replace: (a: A, s: S) => S;
replaceResult: (a: A, s: S) => Result.Result<S, string>;
compose: { (this: Iso<S, ReadonlyArray<A>>, that: Iso<ReadonlyArray<A>, B>): Iso<S, B>; (this: Lens<S, ReadonlyArray<A>>, that: Lens<ReadonlyArray<A>, B>): Lens<S, B>; (this: Prism<S, ReadonlyArray<A>>, that: Prism<ReadonlyArray<A>, B>): Prism<S, B…;
modify: (f: (a: ReadonlyArray<A>) => ReadonlyArray<A>) => (s: S) => S;
key: { <S, A extends object, Key extends keyof A>(this: Lens<S, A>, key: Key, ..._err: ForbidUnion<A, 'cannot use `key` on a union type'>): Lens<S, A[Key]>; <S, A extends object, Key extends keyof A>(this: Optional<S, A>, key: Key, ..._err: For…;
optionalKey: { <S, A extends object, Key extends keyof A>(this: Lens<S, A>, key: Key, ..._err: ForbidUnion<A, 'cannot use `optionalKey` on a union type'>): Lens<S, A[Key] | undefined>; <S, A extends object, Key extends keyof A>(this: Optional<S, A>, ke…;
check: { <S, A>(this: Prism<S, A>, checks_0: SchemaAST.Check<A>, ...checks: Array<SchemaAST.Check<A>>): Prism<S, A>; <S, A>(this: Optional<S, A>, checks_0: SchemaAST.Check<A>, ...checks: Array<SchemaAST.Check<A>>): Optional<S, A> };
refine: { <S, A, B extends A>(this: Prism<S, A>, refinement: (a: A) => a is B, annotations?: Schema.Annotations.Filter): Prism<S, B>; <S, A, B extends A>(this: Optional<S, A>, refinement: (a: A) => a is B, annotations?: Schema.Annotations.Filter):…;
tag: { <S, A extends { readonly _tag: SchemaAST.LiteralValue }, Tag extends A['_tag']>(this: Prism<S, A>, tag: Tag): Prism<S, Extract<A, { readonly _tag: Tag }>>; <S, A extends { readonly _tag: SchemaAST.LiteralValue }, Tag extends A['_tag']>(t…;
at: <S, A extends object, Key extends keyof A>(this: Optional<S, A>, key: Key, ..._err: ForbidUnion<A, 'cannot use `at` on a union type'>) => Optional<S, A[Key]>;
pick: { <S, A, Keys extends ReadonlyArray<keyof A>>(this: Lens<S, A>, keys: Keys, ..._err: ForbidUnion<A, 'cannot use `pick` on a union type'>): Lens<S, Pick<A, Keys[number]>>; <S, A, Keys extends ReadonlyArray<keyof A>>(this: Optional<S, A>, ke…;
omit: { <S, A, Keys extends ReadonlyArray<keyof A>>(this: Lens<S, A>, keys: Keys, ..._err: ForbidUnion<A, 'cannot use `omit` on a union type'>): Lens<S, Omit<A, Keys[number]>>; <S, A, Keys extends ReadonlyArray<keyof A>>(this: Optional<S, A>, ke…;
notUndefined: { (): Prism<S, ReadonlyArray<A>>; (): Optional<S, ReadonlyArray<A>> };
forEach: <S, A, B>(this: Traversal<S, A>, f: (iso: Iso<A, A>) => Optional<A, B>) => Traversal<S, B>;
modifyAll: <S, A>(this: Traversal<S, A>, f: (a: A) => A) => (s: S) => S;
}
traversal.Optional<S, readonly A[]>.getResult: (s: S) => Result.Result<A, string>Attempts to read the focus A from the whole S. Returns
Result.Success<A> when the focus exists, or
Result.Failure<string> with a descriptive error otherwise.
getResult(s: Ss), {
onFailure: (error: string) => never[]onFailure: () => [],
onSuccess: (ok: readonly A[]) => A[]onSuccess: (as: readonly A[]as) => [...as: readonly A[]as]
})
}