Hyperlinkv0.8.0-beta.28

Optic

Optic.getAllfunctioneffect/Optic.ts:1425
<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: []
TraversalTraversal
Source effect/Optic.ts:14257 lines
export function getAll<S, A>(traversal: Traversal<S, A>): (s: S) => Array<A> {
  return (s) =>
    Result.match(traversal.getResult(s), {
      onFailure: () => [],
      onSuccess: (as) => [...as]
    })
}