Merge pull request #203 from AlexKnauth/lazy-lens
add lazy-lens and rec-lens
This commit is contained in:
commit
87d9a2a4f4
29
unstable/lens/lazy.rkt
Normal file
29
unstable/lens/lazy.rkt
Normal file
|
@ -0,0 +1,29 @@
|
|||
#lang sweet-exp racket/base
|
||||
|
||||
provide lazy-lens
|
||||
rec-lens
|
||||
|
||||
require fancy-app lens/private/base/main racket/promise
|
||||
module+ test
|
||||
require rackunit "if.rkt" "isomorphism/data.rkt" "mapper.rkt"
|
||||
|
||||
(define-syntax-rule (lazy-lens expr)
|
||||
(let ([p (delay expr)])
|
||||
(make-lens (lens-view (force p) _) (lens-set (force p) _ _))))
|
||||
|
||||
(define-syntax-rule (rec-lens name expr)
|
||||
(letrec ([name (lazy-lens expr)])
|
||||
name))
|
||||
|
||||
module+ test
|
||||
(define (tree-mapper-lens item-lens)
|
||||
(rec-lens the-tree-lens
|
||||
(lens-cond [list? (mapper-lens the-tree-lens)]
|
||||
[else item-lens])))
|
||||
(check-equal? (lens-view (tree-mapper-lens symbol->string-lens) '(a (b (() c)) (d)))
|
||||
'("a" ("b" (() "c")) ("d")))
|
||||
(check-equal? (lens-set (tree-mapper-lens symbol->string-lens)
|
||||
'(a (b (() c)) (d))
|
||||
'("hay" ("bee" (() "sea")) ("deep")))
|
||||
'(hay (bee (() sea)) (deep)))
|
||||
|
33
unstable/lens/lazy.scrbl
Normal file
33
unstable/lens/lazy.scrbl
Normal file
|
@ -0,0 +1,33 @@
|
|||
#lang scribble/manual
|
||||
|
||||
@(require lens/private/doc-util/main)
|
||||
|
||||
@title{Lazy lenses and recursive lenses}
|
||||
|
||||
@defmodule[unstable/lens/lazy]
|
||||
|
||||
@defform[(lazy-lens lens-expr)]{
|
||||
Creates a lazy lens that lazily evaluates @racket[lens-expr] and uses it to view
|
||||
and set the target.
|
||||
@lens-unstable-examples[
|
||||
(define lazy-first-lens
|
||||
(lazy-lens (begin (displayln "evaluating") first-lens)))
|
||||
lazy-first-lens
|
||||
(lens-view lazy-first-lens '(1 2 3))
|
||||
(lens-set lazy-first-lens '(1 2 3) 'a)
|
||||
]}
|
||||
|
||||
@defform[(rec-lens rec-id lens-expr)]{
|
||||
Creates a potentially recursive lens, where @racket[lens-expr] can refer to
|
||||
@racket[rec-id] as a lazy version of itself.
|
||||
@lens-unstable-examples[
|
||||
(define (tree-mapper-lens item-lens)
|
||||
(rec-lens the-tree-lens
|
||||
(lens-cond [list? (mapper-lens the-tree-lens)]
|
||||
[else item-lens])))
|
||||
(lens-view (tree-mapper-lens symbol->string-lens) '(a (b (() c)) (d)))
|
||||
(lens-set (tree-mapper-lens symbol->string-lens)
|
||||
'(a (b (() c)) (d))
|
||||
'("hay" ("bee" (() "sea")) ("deep")))
|
||||
]}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
"if.rkt"
|
||||
"isomorphism.rkt"
|
||||
"join-assoc.rkt"
|
||||
"lazy.rkt"
|
||||
"mapper.rkt"
|
||||
"match.rkt"
|
||||
"set-filterer.rkt"
|
||||
|
|
|
@ -17,6 +17,7 @@ this library being backwards-compatible.
|
|||
"if.scrbl"
|
||||
"isomorphism.scrbl"
|
||||
"join-assoc.scrbl"
|
||||
"lazy.scrbl"
|
||||
"mapper.scrbl"
|
||||
"match.scrbl"
|
||||
"set-filterer.scrbl"
|
||||
|
|
Loading…
Reference in New Issue
Block a user