diff --git a/unstable/lens/join.rkt b/unstable/lens/join.rkt index 3420773..ae51267 100644 --- a/unstable/lens/join.rkt +++ b/unstable/lens/join.rkt @@ -84,8 +84,11 @@ (λ (tgt) (f tgt)) (λ (tgt v) (f-inv v)))) +(define (list->immutable-vector lst) + (apply vector-immutable lst)) + (define list->vector-lens - (inverse-function-lens list->vector vector->list)) + (inverse-function-lens list->immutable-vector vector->list)) (module+ test (define vector-first-third-fifth-lens @@ -94,6 +97,7 @@ fifth-lens)) (check-equal? (lens-view vector-first-third-fifth-lens '(a b c d e f)) #(a c e)) + (check-pred immutable? (lens-view vector-first-third-fifth-lens '(a b c d e f))) (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/main.rkt b/unstable/lens/main.rkt index 75a3f97..d755f4d 100644 --- a/unstable/lens/main.rkt +++ b/unstable/lens/main.rkt @@ -4,6 +4,7 @@ "join.rkt" "list.rkt" "hash.rkt" + "vector.rkt" "string.rkt" "view-set.rkt" "sublist.rkt" @@ -17,6 +18,7 @@ "join.rkt" "list.rkt" "hash.rkt" + "vector.rkt" "string.rkt" "view-set.rkt" "sublist.rkt" diff --git a/unstable/lens/main.scrbl b/unstable/lens/main.scrbl index 3b5daf3..33d10b2 100644 --- a/unstable/lens/main.scrbl +++ b/unstable/lens/main.scrbl @@ -13,6 +13,7 @@ this library being backwards-compatible. @include-section["join.scrbl"] @include-section["list.scrbl"] @include-section["hash.scrbl"] +@include-section["vector.scrbl"] @include-section["string.scrbl"] @include-section["syntax.scrbl"] @include-section["sublist.scrbl"] diff --git a/unstable/lens/vector.rkt b/unstable/lens/vector.rkt new file mode 100644 index 0000000..7eefbd6 --- /dev/null +++ b/unstable/lens/vector.rkt @@ -0,0 +1,50 @@ +#lang racket/base + +(provide vector-ref-lens + vector-ref-nested-lens + vector-pluck-lens + ) + +(require fancy-app + lens/base/main + "arrow.rkt" + "join.rkt" + ) +(module+ test + (require rackunit)) + +(define (vector-ref-lens i) + (make-lens + (vector-ref _ i) + (vector-set _ i _))) + +(define (vector-set v i x) + (vector->immutable-vector + (build-vector (vector-length v) + (λ (j) + (if (= i j) + x + (vector-ref v j)))))) + +(define (vector-ref-nested-lens . is) + (apply lens-thrush (map vector-ref-lens is))) + +(define (vector-pluck-lens . is) + (apply lens-join/vector (map vector-ref-lens is))) + + +(module+ test + (check-equal? (lens-view (vector-ref-lens 0) #(a b c)) 'a) + (check-equal? (lens-view (vector-ref-lens 1) #(a b c)) 'b) + (check-equal? (lens-view (vector-ref-lens 2) #(a b c)) 'c) + (check-equal? (lens-set (vector-ref-lens 0) #(a b c) "A") #("A" b c)) + (check-equal? (lens-set (vector-ref-lens 1) #(a b c) "B") #(a "B" c)) + (check-equal? (lens-set (vector-ref-lens 2) #(a b c) "C") #(a b "C")) + (check-equal? (lens-transform (vector-ref-nested-lens 2 1) #(a #(b c) #(d e f)) symbol->string) + #(a #(b c) #(d "e" f))) + (define 1-5-6-lens (vector-pluck-lens 1 5 6)) + (check-equal? (lens-view 1-5-6-lens #(a b c d e f g)) + #(b f g)) + (check-equal? (lens-set 1-5-6-lens #(a b c d e f g) #(1 2 3)) + #(a 1 c d e 2 3)) + ) diff --git a/unstable/lens/vector.scrbl b/unstable/lens/vector.scrbl new file mode 100644 index 0000000..8730b11 --- /dev/null +++ b/unstable/lens/vector.scrbl @@ -0,0 +1,31 @@ +#lang scribble/manual + +@(require lens/doc-util/main) + +@title{Vector lenses} + +@defproc[(vector-ref-lens [i exact-nonnegative-integer?]) lens?]{ +Returns a lens that views an element of a vector. +@lenses-unstable-examples[ + (lens-view (vector-ref-lens 2) #(a b c d)) + (lens-set (vector-ref lens 2) #(a b c d) "sea") +]} + +@defproc[(vector-ref-nested-lens [i exact-nonnegative-integer?] ...) lens?]{ +Like @racket[list-ref-nested-lens], but for vectors. +Equivalent to @racket[(lens-thrush (vector-ref-lens i) ...)]. +@lenses-unstable-examples[ + (lens-view (vector-ref-nested-lens 2 1) #(a b #(s i) d)) + (lens-set (vector-ref-nested-lens 2 1) #(a b #(s i) d) "eye") +]} + +@defproc[(vector-pluck-lens [i exact-nonnegative-integer?] ...) lens?]{ +Like @racket[list-refs-lens], but for vectors. +Equivalent to @racket[(lens-join/vector (vector-ref-lens i) ...)]. +@lenses-unstable-examples[ + (define 1-5-6-lens (vector-pluck-lens 1 5 6)) + (check-equal? (lens-view 1-5-6-lens #(a b c d e f g)) + #(b f g)) + (check-equal? (lens-set 1-5-6-lens #(a b c d e f g) #(1 2 3)) + #(a 1 c d e 2 3)) +]}