tur/lens
stdlib/lens.tur
first-class functional lenses (getter/setter optics).
Since: constrained-hkt-forall slice 4
defstruct
Lens
(defstruct Lens :copy [S A])
defn
lens
(lens [S A])
build a Lens from a getter and a setter.
Parameters
| get | reads the focused A out of the whole S | |
| put | returns a new S with the focused A replaced |
Returns
A Lens S A pairing `get` and `put`.
Example
(lens (fn [p : Point] : int (.x p))
(fn [nx : int p : Point] : Point
(make-struct Point :x nx :y (.y p))))
Since: constrained-hkt-forall slice 4
defn
view
(view [S A])
read the focused A out of the whole S.
Parameters
| l | the lens | |
| s | the whole value |
Returns
The focused part A.
Example
(view point-x p) ; => (.x p)
Since: constrained-hkt-forall slice 4
defn
set
(set [S A])
replace the focused A, returning the updated S.
Parameters
| l | the lens | |
| a | the new focused value | |
| s | the whole value |
Returns
A new S with the focus replaced by `a`.
Example
(set point-x 42 p) ; => p with x = 42
Since: constrained-hkt-forall slice 4
defn
over
(over [S A])
apply a function to the focused A, returning the updated S.
Parameters
| l | the lens | |
| f | transforms the focused value | |
| s | the whole value |
Returns
A new S with the focus mapped through `f`.
Example
(over point-x (fn [v : int] : int (* v 10)) p) ; => p with x *= 10
Since: constrained-hkt-forall slice 4