<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>Creates an Iso from a pair of conversion functions.
When to use
Use when you have two pure conversion functions that preserve all information
between S and A.
Details
The returned optic can be composed with any other optic.
Example (Wrapping and unwrapping a branded type)
import { Optic } from "effect"
type Meters = { readonly value: number }
const meters = Optic.makeIso<Meters, number>(
(m) => m.value,
(n) => ({ value: n })
)
console.log(meters.get({ value: 100 }))
// Output: 100
console.log(meters.set(42))
// Output: { value: 42 }export function function makeIso<S, A>(
get: (s: S) => A,
set: (a: A) => S
): Iso<S, A>
Creates an
Iso
from a pair of conversion functions.
When to use
Use when you have two pure conversion functions that preserve all information
between S and A.
Details
The returned optic can be composed with any other optic.
Example (Wrapping and unwrapping a branded type)
import { Optic } from "effect"
type Meters = { readonly value: number }
const meters = Optic.makeIso<Meters, number>(
(m) => m.value,
(n) => ({ value: n })
)
console.log(meters.get({ value: 100 }))
// Output: 100
console.log(meters.set(42))
// Output: { value: 42 }
makeIso<function (type parameter) S in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>S, function (type parameter) A in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>A>(get: (s: S) => Aget: (s: Ss: function (type parameter) S in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>S) => function (type parameter) A in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>A, set: (a: A) => Sset: (a: Aa: function (type parameter) A in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>A) => function (type parameter) S in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>S): interface Iso<in out S, in out 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
Iso<function (type parameter) S in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>S, function (type parameter) A in makeIso<S, A>(get: (s: S) => A, set: (a: A) => S): Iso<S, A>A> {
return function make(node: Node): anymake(new constructor IsoNode<S, A>(get: (s: S) => A, set: (a: A) => S): IsoNode<S, A>IsoNode(get: (s: S) => Aget, set: (a: A) => Sset))
}