<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>Creates a Lens from a getter and a replacer.
When to use
Use when you can always extract A from S and produce a new S by
substituting a new A.
Details
replace(a, s)should return a structurally newSwithain place of the old focus.
Example (Focusing on the first element of a pair)
import { Optic } from "effect"
const _first = Optic.makeLens<readonly [string, number], string>(
(pair) => pair[0],
(s, pair) => [s, pair[1]]
)
console.log(_first.get(["hello", 42]))
// Output: "hello"
console.log(_first.replace("world", ["hello", 42]))
// Output: ["world", 42]Source effect/Optic.ts:1843 lines
export function function makeLens<S, A>(
get: (s: S) => A,
replace: (a: A, s: S) => S
): Lens<S, A>
Creates a
Lens
from a getter and a replacer.
When to use
Use when you can always extract A from S and produce a new S by
substituting a new A.
Details
replace(a, s) should return a structurally new S with a in place
of the old focus.
Example (Focusing on the first element of a pair)
import { Optic } from "effect"
const _first = Optic.makeLens<readonly [string, number], string>(
(pair) => pair[0],
(s, pair) => [s, pair[1]]
)
console.log(_first.get(["hello", 42]))
// Output: "hello"
console.log(_first.replace("world", ["hello", 42]))
// Output: ["world", 42]
makeLens<function (type parameter) S in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>S, function (type parameter) A in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>A>(get: (s: S) => Aget: (s: Ss: function (type parameter) S in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>S) => function (type parameter) A in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>A, replace: (a: A, s: S) => Sreplace: (a: Aa: function (type parameter) A in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>A, s: Ss: function (type parameter) S in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>S) => function (type parameter) S in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>S): interface Lens<in out S, in out A>Focuses on exactly one part A inside a whole S.
When to use
Use when you always have a value to read and need the original S to produce
the updated whole, unlike Iso.
Details
get(s) always succeeds and returns A.
replace(a, s) returns a new S with the focused part replaced.
- Extends
Optional
.
- Composing a Lens with a
Prism
or
Optional
produces an
Optional
.
Example (Focusing on a struct field)
import { Optic } from "effect"
type Person = { readonly name: string; readonly age: number }
const _name = Optic.id<Person>().key("name")
console.log(_name.get({ name: "Alice", age: 30 }))
// Output: "Alice"
Lens<function (type parameter) S in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>S, function (type parameter) A in makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Lens<S, A>A> {
return function make(node: Node): anymake(new constructor LensNode<S, A>(get: (s: S) => A, set: (a: A, s: S) => S): LensNode<S, A>LensNode(get: (s: S) => Aget, replace: (a: A, s: S) => Sreplace))
}