<S>(): Iso<S, S>Iso that focuses on the whole value unchanged.
When to use
Use when you need to start an optic chain with a focus on the whole value.
Details
get(s)returnss.set(a)returnsa.- Singleton — every call returns the same instance.
Example (Starting an optic chain)
import { Optic } from "effect"
type S = { readonly x: number }
const _x = Optic.id<S>().key("x")
console.log(_x.get({ x: 42 }))
// Output: 42IsoIso
Source effect/Optic.ts:14703 lines
export function function id<S>(): Iso<S, S>Iso that focuses on the whole value unchanged.
When to use
Use when you need to start an optic chain with a focus on the whole value.
Details
get(s) returns s.
set(a) returns a.
- Singleton — every call returns the same instance.
Example (Starting an optic chain)
import { Optic } from "effect"
type S = { readonly x: number }
const _x = Optic.id<S>().key("x")
console.log(_x.get({ x: 42 }))
// Output: 42
id<function (type parameter) S in id<S>(): Iso<S, S>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 id<S>(): Iso<S, S>S, function (type parameter) S in id<S>(): Iso<S, S>S> {
return const identityIso: anyidentityIso
}Referenced by 2 symbols