Hyperlinkv0.8.0-beta.28

Optic

Optic.makePrismfunctioneffect/Optic.ts:274
<S, A>(
  getResult: (s: S) => Result.Result<A, string>,
  set: (a: A) => S
): Prism<S, A>

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

  • getResult should return Result.fail(message) on mismatch.

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"
constructorsPrismfromChecks
Source effect/Optic.ts:2743 lines
export function makePrism<S, A>(getResult: (s: S) => Result.Result<A, string>, set: (a: A) => S): Prism<S, A> {
  return make(new PrismNode(getResult, set))
}
Referenced by 4 symbols