Hyperlinkv0.8.0-beta.28

Optic

Optic.Isointerfaceeffect/Optic.ts:66
Iso<S, A>

A lossless, reversible conversion between types S and A.

When to use

Use when you have a pair of functions that convert back and forth without losing information (e.g. Record ↔ entries, Celsius ↔ Fahrenheit).

  • You want the strongest optic that can be composed with any other.

Details

  • get(s) always succeeds and returns an A.
  • set(a) always succeeds and returns an S.
  • get(set(a)) === a and set(get(s)) equals s (round-trip laws).
  • Extends both Lens and Prism.

Example (Converting between Celsius and Fahrenheit)

import { Optic } from "effect"

const fahrenheit = Optic.makeIso<number, number>(
  (c) => c * 9 / 5 + 32,
  (f) => (f - 32) * 5 / 9
)

console.log(fahrenheit.get(100))
// Output: 212

console.log(fahrenheit.set(32))
// Output: 0
Source effect/Optic.ts:661 lines
export interface Iso<in out S, in out A> extends Lens<S, A>, Prism<S, A> {}
Referenced by 8 symbols