rename list-compound-lens and hash-compound-lens to lens-join/...
This commit is contained in:
parent
d05e344a24
commit
734bfbaa69
|
@ -10,14 +10,14 @@
|
||||||
|
|
||||||
(provide
|
(provide
|
||||||
(contract-out
|
(contract-out
|
||||||
[compound-list-lens (->* () #:rest (listof lens?) lens?)]
|
[lens-join/list (->* () #:rest (listof lens?) lens?)]
|
||||||
[compound-hash-lens (->* () #:rest (listof2 any/c lens?) lens?)]))
|
[lens-join/hash (->* () #:rest (listof2 any/c lens?) lens?)]))
|
||||||
|
|
||||||
|
|
||||||
(define (zip xs ys)
|
(define (zip xs ys)
|
||||||
(append-map list xs ys))
|
(append-map list xs ys))
|
||||||
|
|
||||||
(define (compound-list-lens . lenses)
|
(define (lens-join/list . lenses)
|
||||||
(define (get target)
|
(define (get target)
|
||||||
(apply lens-view/list target lenses))
|
(apply lens-view/list target lenses))
|
||||||
(define (set target new-views)
|
(define (set target new-views)
|
||||||
|
@ -27,16 +27,16 @@
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
(define first-third-fifth-lens
|
(define first-third-fifth-lens
|
||||||
(compound-list-lens first-lens
|
(lens-join/list first-lens
|
||||||
third-lens
|
third-lens
|
||||||
fifth-lens))
|
fifth-lens))
|
||||||
(check-equal? (lens-view first-third-fifth-lens '(a b c d e f))
|
(check-equal? (lens-view first-third-fifth-lens '(a b c d e f))
|
||||||
'(a c e))
|
'(a c e))
|
||||||
(check-equal? (lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3))
|
(check-equal? (lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3))
|
||||||
'(1 b 2 d 3 f)))
|
'(1 b 2 d 3 f)))
|
||||||
(define first-first-lens
|
(define first-first-lens
|
||||||
(compound-list-lens first-lens
|
(lens-join/list first-lens
|
||||||
first-lens))
|
first-lens))
|
||||||
|
|
||||||
|
|
||||||
(define (value-list->hash keys vs)
|
(define (value-list->hash keys vs)
|
||||||
|
@ -55,9 +55,9 @@
|
||||||
'((a b c) (1 2 3) (FOO BAR BAZ))))
|
'((a b c) (1 2 3) (FOO BAR BAZ))))
|
||||||
|
|
||||||
|
|
||||||
(define (compound-hash-lens . keys/lenses)
|
(define (lens-join/hash . keys/lenses)
|
||||||
(match-define (list keys lenses) (split-slice 2 keys/lenses))
|
(match-define (list keys lenses) (split-slice 2 keys/lenses))
|
||||||
(define list-lens (apply compound-list-lens lenses))
|
(define list-lens (apply lens-join/list lenses))
|
||||||
(define (get target)
|
(define (get target)
|
||||||
(value-list->hash keys (lens-view list-lens target)))
|
(value-list->hash keys (lens-view list-lens target)))
|
||||||
(define (set target new-view-hash)
|
(define (set target new-view-hash)
|
||||||
|
@ -65,8 +65,8 @@
|
||||||
(make-lens get set))
|
(make-lens get set))
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
(define a-b-lens (compound-hash-lens 'b third-lens
|
(define a-b-lens (lens-join/hash 'b third-lens
|
||||||
'a first-lens))
|
'a first-lens))
|
||||||
(check-equal? (lens-view a-b-lens '(1 2 3))
|
(check-equal? (lens-view a-b-lens '(1 2 3))
|
||||||
(hash 'a 1 'b 3))
|
(hash 'a 1 'b 3))
|
||||||
(check-equal? (lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200))
|
(check-equal? (lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200))
|
|
@ -3,11 +3,11 @@
|
||||||
@(require lens/doc-util/main)
|
@(require lens/doc-util/main)
|
||||||
|
|
||||||
|
|
||||||
@title{Compound Lenses}
|
@title{Joining Lenses}
|
||||||
|
|
||||||
@defmodule[unstable/lens/compound]
|
@defmodule[unstable/lens/join]
|
||||||
|
|
||||||
@defproc[(compound-list-lens [lens lens?] ...) lens?]{
|
@defproc[(lens-join/list [lens lens?] ...) lens?]{
|
||||||
Constructs a lens that combines the view of each
|
Constructs a lens that combines the view of each
|
||||||
@racket[lens] into a list of views. This lens can
|
@racket[lens] into a list of views. This lens can
|
||||||
be used to view and set a list of values in a single
|
be used to view and set a list of values in a single
|
||||||
|
@ -15,22 +15,22 @@
|
||||||
setting the later lenses override the earlier ones.
|
setting the later lenses override the earlier ones.
|
||||||
@lenses-unstable-examples[
|
@lenses-unstable-examples[
|
||||||
(define first-third-fifth-lens
|
(define first-third-fifth-lens
|
||||||
(compound-list-lens first-lens
|
(lens-join/list first-lens
|
||||||
third-lens
|
third-lens
|
||||||
fifth-lens))
|
fifth-lens))
|
||||||
(lens-view first-third-fifth-lens '(a b c d e f))
|
(lens-view first-third-fifth-lens '(a b c d e f))
|
||||||
(lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3))
|
(lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3))
|
||||||
]}
|
]}
|
||||||
|
|
||||||
@defproc[(compound-hash-lens [key any/c] [lens lens?] ... ...) lens?]{
|
@defproc[(lens-join/hash [key any/c] [lens lens?] ... ...) lens?]{
|
||||||
Constructs a lens that combines the view of each
|
Constructs a lens that combines the view of each
|
||||||
@racket[lens] into a hash of views with @racket[key]s
|
@racket[lens] into a hash of views with @racket[key]s
|
||||||
as the hash keys. In the same manner as @racket[compound-list-lens],
|
as the hash keys. In the same manner as @racket[lens-join/list],
|
||||||
if lenses share views later lenses take precedence when
|
if lenses share views later lenses take precedence when
|
||||||
setting.
|
setting.
|
||||||
@lenses-unstable-examples[
|
@lenses-unstable-examples[
|
||||||
(define a-b-lens (compound-hash-lens 'a first-lens
|
(define a-b-lens (lens-join/hash 'a first-lens
|
||||||
'b third-lens))
|
'b third-lens))
|
||||||
(lens-view a-b-lens '(1 2 3))
|
(lens-view a-b-lens '(1 2 3))
|
||||||
(lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200))
|
(lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200))
|
||||||
]}
|
]}
|
|
@ -1,7 +1,7 @@
|
||||||
#lang racket
|
#lang racket
|
||||||
|
|
||||||
(require lens
|
(require lens
|
||||||
"compound.rkt")
|
"join.rkt")
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
(require rackunit))
|
(require rackunit))
|
||||||
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
|
|
||||||
(define (list-refs-lens . indices)
|
(define (list-refs-lens . indices)
|
||||||
(apply compound-list-lens (map list-ref-lens indices)))
|
(apply lens-join/list (map list-ref-lens indices)))
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
(define 1-5-6-lens (list-refs-lens 1 5 6))
|
(define 1-5-6-lens (list-refs-lens 1 5 6))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#lang racket
|
#lang racket
|
||||||
|
|
||||||
(require "syntax.rkt"
|
(require "syntax.rkt"
|
||||||
"compound.rkt"
|
"join.rkt"
|
||||||
"list.rkt"
|
"list.rkt"
|
||||||
"hash.rkt"
|
"hash.rkt"
|
||||||
"view-set.rkt"
|
"view-set.rkt"
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
)
|
)
|
||||||
|
|
||||||
(provide (all-from-out "syntax.rkt"
|
(provide (all-from-out "syntax.rkt"
|
||||||
"compound.rkt"
|
"join.rkt"
|
||||||
"list.rkt"
|
"list.rkt"
|
||||||
"hash.rkt"
|
"hash.rkt"
|
||||||
"view-set.rkt"
|
"view-set.rkt"
|
||||||
|
|
|
@ -10,7 +10,7 @@ may change in future releases. Do not depend on
|
||||||
this library being backwards-compatible.
|
this library being backwards-compatible.
|
||||||
|
|
||||||
@include-section["view-set.scrbl"]
|
@include-section["view-set.scrbl"]
|
||||||
@include-section["compound.scrbl"]
|
@include-section["join.scrbl"]
|
||||||
@include-section["list.scrbl"]
|
@include-section["list.scrbl"]
|
||||||
@include-section["hash.scrbl"]
|
@include-section["hash.scrbl"]
|
||||||
@include-section["syntax.scrbl"]
|
@include-section["syntax.scrbl"]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user