From 5ceb430c88e085e63dd74657b788cabff793db70 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Wed, 15 Jul 2015 14:36:01 -0400 Subject: [PATCH] add lens-join/vector --- unstable/lens/join.rkt | 26 +++++++++++++++++++++++++- unstable/lens/join.scrbl | 11 +++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/unstable/lens/join.rkt b/unstable/lens/join.rkt index 39097c9..10c9a21 100644 --- a/unstable/lens/join.rkt +++ b/unstable/lens/join.rkt @@ -11,7 +11,9 @@ (provide (contract-out [lens-join/list (->* () #:rest (listof lens?) lens?)] - [lens-join/hash (->* () #:rest (listof2 any/c lens?) lens?)])) + [lens-join/hash (->* () #:rest (listof2 any/c lens?) lens?)] + [lens-join/vector (->* () #:rest (listof lens?) lens?)] + )) (define (zip xs ys) @@ -71,3 +73,25 @@ (hash 'a 1 'b 3)) (check-equal? (lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200)) '(100 2 200))) + + +(define (lens-join/vector . lenses) + (lens-compose list->vector-lens (apply lens-join/list lenses))) + +(define (inverse-function-lens f f-inv) + (make-lens + (λ (tgt) (f tgt)) + (λ (tgt v) (f-inv v)))) + +(define list->vector-lens + (inverse-function-lens list->vector vector->list)) + +(module+ test + (define vector-first-third-fifth-lens + (lens-join/vector first-lens + third-lens + fifth-lens)) + (check-equal? (lens-view vector-first-third-fifth-lens '(a b c d e f)) + #(a c e)) + (check-equal? (lens-set vector-first-third-fifth-lens '(a b c d e f) #(1 2 3)) + '(1 b 2 d 3 f))) diff --git a/unstable/lens/join.scrbl b/unstable/lens/join.scrbl index 9a7d2f4..6b92582 100644 --- a/unstable/lens/join.scrbl +++ b/unstable/lens/join.scrbl @@ -34,3 +34,14 @@ (lens-view a-b-lens '(1 2 3)) (lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200)) ]} + +@defproc[(lens-join/vector [lens lens?] ...) lens?]{ + Like @racket[lens-join/list], except the view is a vector, not a list. + @lenses-unstable-examples[ + (define vector-first-third-fifth-lens + (lens-join/vector first-lens + third-lens + fifth-lens)) + (lens-view vector-first-third-fifth-lens '(a b c d e f)) + (lens-set vector-first-third-fifth-lens '(a b c d e f) #(1 2 3)) +]}