effect-io-ai

Package: effect
Module: Optic

Optic.makePrism

Creates a Prism from a fallible getter and an infallible setter.

When to use

Use when reading can fail (the part may not exist in S), but building S from A always succeeds.

Details

Example (Parsing a string to a number)

import { Optic, Result } from "effect"

const numeric = Optic.makePrism<string, number>(
  (s) => {
    const n = Number(s)
    return Number.isNaN(n) ? Result.fail("not a number") : Result.succeed(n)
  },
  String
)

console.log(Result.isSuccess(numeric.getResult("42")))
// Output: true

console.log(numeric.set(42))
// Output: "42"

See

Signature

declare const makePrism: <S, A>(getResult: (s: S) => Result.Result<A, string>, set: (a: A) => S) => Prism<S, A>

Source

Since v4.0.0