Optics
For a comprehensive understanding of the optics concept, you can refer to arxiv 2001.07488v3.
Lens
Optical components for focusing on specific parts of data structures.
Lens.of()
Constructs a lens.
- Type
- Details
The first argument is a getter function, and the second argument is a setter function. The function returns a lens that can be used to get, set, or modify a specific part of a data structure.
- Example
Lens.of((s) => s.a, assoc('a'));Lens.of((s) => s.a, assoc('a'));Lens.bind()
Binds an element directly to a lens.
- Type
Overload 1
Overload 2
- Details
Creates a lens that focuses on the specified element or path within a data structure.
- Example
Lens.bind('a');
Lens.bind(['a', 'b']);Lens.bind('a');
Lens.bind(['a', 'b']);view
Returns the part of a data structure that a lens is focused on.
- Type
- Details
Takes a lens and a data structure as arguments and returns the part of the data structure that the lens is focused on.
- Example
const lens = Lens.bind('a');
view(lens, { a: 1 }); // 1const lens = Lens.bind('a');
view(lens, { a: 1 }); // 1set
Sets the value of the part of a data structure that a lens is focused on.
- Type
- Details
Takes a lens, a value to set, and a data structure as arguments. It returns a new data structure with the value set at the focus of the lens.
- Example
const lens = Lens.bind('a');
set(lens, 2, { a: 1 }); // { a: 2 }const lens = Lens.bind('a');
set(lens, 2, { a: 1 }); // { a: 2 }over
Applies a function to the part of a data structure that a lens is focused on.
- Type
- Details
Takes a lens, a function to apply, and a data structure as arguments. It returns a new data structure where the function has been applied to the focus of the lens.
- Example
const lens = Lens.bind('a');
over(lens, add(1), { a: 1 }); // { a: 2 }const lens = Lens.bind('a');
over(lens, add(1), { a: 1 }); // { a: 2 }