1 Core Lens Forms
1.1 Lens Construction
Examples: | ||||||||||||||
|
syntax
(let-lens (view-id context-id) lens-expr target-expr body ...)
Example: | ||||||
|
1.2 Viewing and Setting
Example: | ||
|
procedure
(lens-set lens target new-view) → target/c
lens : lens? target : target/c new-view : view/c
Example: | ||
|
procedure
(lens-view/list target lens ...) → view/c
target : target/c lens : lens?
Example: | ||||
|
procedure
(lens-set/list target lens new-view ... ...) → target/c
target : target/c lens : lens? new-view : view/c
Examples: | ||||||||||
|
1.3 Lens Laws
While make-lens allows lenses to be constructed from arbitrary getters and setters, these getters and setters should obey some algebraic laws in order for a lens to be a proper lens. A lens that does not obey the lens laws for all values it can focus on is an improper lens. The lens laws formalize some standard intuitions for how getters and setters "ought" to work. The laws for lenses are:
- Purity - Viewing and setting with a lens L must both be pure functions. Formally, the two functionsmust be pure for tagets and views of L.
- Set-Get Consistency - For all targets and views, setting a target and then viewing it returns the set value. Formally, given a target T of a lens L, the functionmust be identical to the identity function for views of L with respect to a reasonable definition of equality for views of L.
- Get-Set Consistency - For all targets and views, getting a view of a target and then setting the target’s view to the view you just got does nothing. Formally, given a target T of a lens L, the expressionmust be equal to T with respect to a reasonable definition of equality for targets of L.
- Last Set Wins - For all targets and views, if you set the same target to two different views, the one you set last applies. Formally, given a target T of a lens L and two views v-first and v-second of L, the expressionmust be equal to v-second with respect to a reasonable definition of equality for views of L.
For those familiar with getters and setters in OO languages, none of these should be surprising other than the requirement that lenses be pure. The purity of lenses allows them to be composed more effectively and reasoned about more easily than an impure equivalent of lenses.
All lenses provided by this library are proper unless otherwise stated. There is no enforcement or contract that lenses constructed with functions from this library will always be proper, but individual functions may provide conditional guarantees about their interactions with improper lenses and the lens laws
1.4 Transforming Values With Lenses
procedure
(lens-transform lens target transformer) → target/c
lens : lens? target : target/c transformer : (-> view/c view/c)
Example: | ||
|
procedure
(lens-transform/list target lens transformer ... ...) → target/c target : target/c lens : lens? transformer : (-> view/c view/c)
Example: | |||||
|