From 614d6876c737c54a6679031ca33a1da0c25f0439 Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Tue, 18 Aug 2015 15:34:01 -0700 Subject: [PATCH] Reorganize and add new folders --- lens/base/main.rkt | 6 +- lens/{base => compound}/compose.rkt | 30 ++--- lens/{base => compound}/compose.scrbl | 0 lens/compound/inverse-function-lens.rkt | 11 ++ lens/compound/join-hash.rkt | 53 +++++++++ lens/compound/join-list.rkt | 35 ++++++ lens/compound/join-string.rkt | 34 ++++++ lens/compound/join-vector.rkt | 35 ++++++ lens/{ => compound}/join.scrbl | 0 lens/compound/main.rkt | 16 +++ lens/compound/thrush.rkt | 30 +++++ lens/hash-pick.rkt | 30 ----- lens/{ => hash}/hash.rkt | 21 ++-- lens/{ => hash}/hash.scrbl | 0 lens/hash/main.rkt | 10 ++ lens/hash/pick.rkt | 30 +++++ lens/{hash-pick.scrbl => hash/pick.scrbl} | 0 lens/join.rkt | 118 -------------------- lens/list/cadr-etc.rkt | 9 +- lens/list/list-ref-take-drop.rkt | 1 + lens/list/main.rkt | 2 + lens/{list.rkt => list/multi.rkt} | 21 ++-- lens/{list.scrbl => list/multi.scrbl} | 0 lens/main.rkt | 20 ++-- lens/stream.rkt | 3 +- lens/string.rkt | 2 +- lens/{struct.rkt => struct/field.rkt} | 11 +- lens/{struct.scrbl => struct/field.scrbl} | 0 lens/struct/main.rkt | 8 ++ {unstable/lens => lens/struct}/struct.rkt | 12 +- {unstable/lens => lens/struct}/struct.scrbl | 0 lens/test-util/test-lens.rkt | 5 +- lens/tests/doc-coverage.rkt | 2 +- lens/util/list-pair-contract.rkt | 22 +++- lens/util/rest-contract.rkt | 11 ++ lens/vector.rkt | 62 ---------- lens/vector/main.rkt | 12 ++ lens/vector/nested.rkt | 27 +++++ lens/vector/pick.rkt | 27 +++++ lens/vector/ref.rkt | 32 ++++++ lens/{vector.scrbl => vector/ref.scrbl} | 0 41 files changed, 463 insertions(+), 285 deletions(-) rename lens/{base => compound}/compose.rkt (58%) rename lens/{base => compound}/compose.scrbl (100%) create mode 100644 lens/compound/inverse-function-lens.rkt create mode 100644 lens/compound/join-hash.rkt create mode 100644 lens/compound/join-list.rkt create mode 100644 lens/compound/join-string.rkt create mode 100644 lens/compound/join-vector.rkt rename lens/{ => compound}/join.scrbl (100%) create mode 100644 lens/compound/main.rkt create mode 100644 lens/compound/thrush.rkt delete mode 100644 lens/hash-pick.rkt rename lens/{ => hash}/hash.rkt (78%) rename lens/{ => hash}/hash.scrbl (100%) create mode 100644 lens/hash/main.rkt create mode 100644 lens/hash/pick.rkt rename lens/{hash-pick.scrbl => hash/pick.scrbl} (100%) delete mode 100644 lens/join.rkt rename lens/{list.rkt => list/multi.rkt} (65%) rename lens/{list.scrbl => list/multi.scrbl} (100%) rename lens/{struct.rkt => struct/field.rkt} (91%) rename lens/{struct.scrbl => struct/field.scrbl} (100%) create mode 100644 lens/struct/main.rkt rename {unstable/lens => lens/struct}/struct.rkt (94%) rename {unstable/lens => lens/struct}/struct.scrbl (100%) create mode 100644 lens/util/rest-contract.rkt delete mode 100644 lens/vector.rkt create mode 100644 lens/vector/main.rkt create mode 100644 lens/vector/nested.rkt create mode 100644 lens/vector/pick.rkt create mode 100644 lens/vector/ref.rkt rename lens/{vector.scrbl => vector/ref.scrbl} (100%) diff --git a/lens/base/main.rkt b/lens/base/main.rkt index 865fe87..b7a4ab3 100644 --- a/lens/base/main.rkt +++ b/lens/base/main.rkt @@ -3,13 +3,11 @@ (require "base.rkt" "view-set.rkt" "transform.rkt" - "identity.rkt" - "compose.rkt") + "identity.rkt") (provide (all-from-out "base.rkt" "view-set.rkt" "transform.rkt" - "identity.rkt" - "compose.rkt")) + "identity.rkt")) diff --git a/lens/base/compose.rkt b/lens/compound/compose.rkt similarity index 58% rename from lens/base/compose.rkt rename to lens/compound/compose.rkt index 4f43f09..b962c38 100644 --- a/lens/base/compose.rkt +++ b/lens/compound/compose.rkt @@ -1,16 +1,15 @@ -#lang racket +#lang racket/base -(require fancy-app - "base.rkt" - "view-set.rkt" - "identity.rkt") +(require racket/contract + racket/list + fancy-app + "../base/main.rkt") (module+ test (require rackunit)) (provide - (contract-out [lens-compose (->* () () #:rest (listof lens?) lens?)] - [lens-thrush (->* () () #:rest (listof lens?) lens?)])) + (contract-out [lens-compose (->* () () #:rest (listof lens?) lens?)])) (define (lens-compose2 sub-lens super-lens) @@ -34,18 +33,7 @@ (define (set-second l v) (list* (first l) v (rest (rest l)))) (define second-lens (make-lens second set-second)) - (define first-of-second-lens (lens-compose first-lens second-lens)) (define test-alist '((a 1) (b 2) (c 3))) - (check-eq? (lens-view first-of-second-lens test-alist) 'b) - (check-equal? (lens-set first-of-second-lens test-alist 'B) - '((a 1) (B 2) (c 3)))) - - -(define (lens-thrush . args) - (apply lens-compose (reverse args))) - -(module+ test - (define first-of-second-lens* (lens-thrush second-lens first-lens)) - (let-lens [val ctxt] first-of-second-lens* test-alist - (check-equal? val 'b) - (check-equal? (ctxt 'B) '((a 1) (B 2) (c 3))))) + (define first-of-second-lens (lens-compose first-lens second-lens)) + (check-equal? (lens-view first-of-second-lens test-alist) 'b) + (check-equal? (lens-set first-of-second-lens test-alist 'B) '((a 1) (B 2) (c 3)))) diff --git a/lens/base/compose.scrbl b/lens/compound/compose.scrbl similarity index 100% rename from lens/base/compose.scrbl rename to lens/compound/compose.scrbl diff --git a/lens/compound/inverse-function-lens.rkt b/lens/compound/inverse-function-lens.rkt new file mode 100644 index 0000000..028ece1 --- /dev/null +++ b/lens/compound/inverse-function-lens.rkt @@ -0,0 +1,11 @@ +#lang racket/base + +(require "../base/main.rkt") + +(provide inverse-function-lens) + + +(define (inverse-function-lens f f-inv) + (make-lens + (λ (tgt) (f tgt)) + (λ (tgt v) (f-inv v)))) diff --git a/lens/compound/join-hash.rkt b/lens/compound/join-hash.rkt new file mode 100644 index 0000000..a393e69 --- /dev/null +++ b/lens/compound/join-hash.rkt @@ -0,0 +1,53 @@ +#lang racket/base + +(require racket/contract + racket/match + unstable/sequence + fancy-app + "../base/main.rkt" + "../util/immutable.rkt" + "../util/list-pair-contract.rkt" + "join-list.rkt") + +(module+ test + (require rackunit + "../list/list-ref-take-drop.rkt")) + +(provide + (contract-out + [lens-join/hash (->* () #:rest (listof2 any/c lens?) (lens/c any/c immutable-hash?))])) + + +(define (value-list->hash keys vs) + (make-immutable-hash (map cons keys vs))) + +(define (split-slice n vs) + (define grouped + (for/list ([group (in-slice n vs)]) + group)) + (define (get-ith i) + (map (list-ref _ i) grouped)) + (build-list n get-ith)) + +(module+ test + (check-equal? (split-slice 3 '(a 1 FOO b 2 BAR c 3 BAZ)) + '((a b c) (1 2 3) (FOO BAR BAZ)))) + + +(define (lens-join/hash . keys/lenses) + (match-define (list keys lenses) (split-slice 2 keys/lenses)) + (define list-lens (apply lens-join/list lenses)) + (define (get target) + (value-list->hash keys (lens-view list-lens target))) + (define (set target new-view-hash) + (lens-set list-lens target (map (hash-ref new-view-hash _) keys))) + (make-lens get set)) + +(module+ test + (define a-b-lens (lens-join/hash 'b third-lens + 'a first-lens)) + (check-equal? (lens-view a-b-lens '(1 2 3)) + (hash 'a 1 'b 3)) + (check-equal? (lens-set a-b-lens '(1 2 3) (hash 'a 100 'b 200)) + '(100 2 200))) + diff --git a/lens/compound/join-list.rkt b/lens/compound/join-list.rkt new file mode 100644 index 0000000..08f9ebc --- /dev/null +++ b/lens/compound/join-list.rkt @@ -0,0 +1,35 @@ +#lang racket/base + +(require racket/list + racket/contract + "../base/main.rkt") + +(module+ test + (require rackunit + "../list/list-ref-take-drop.rkt")) + +(provide + (contract-out + [lens-join/list (->* () #:rest (listof lens?) (lens/c any/c list?))])) + + +(define (zip xs ys) + (append-map list xs ys)) + +(define (lens-join/list . lenses) + (define (get target) + (apply lens-view/list target lenses)) + (define (set target new-views) + (apply lens-set/list target (zip lenses new-views))) + (make-lens get set)) + + +(module+ test + (define first-third-fifth-lens + (lens-join/list first-lens + third-lens + fifth-lens)) + (check-equal? (lens-view first-third-fifth-lens '(a b c d e f)) + '(a c e)) + (check-equal? (lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3)) + '(1 b 2 d 3 f))) diff --git a/lens/compound/join-string.rkt b/lens/compound/join-string.rkt new file mode 100644 index 0000000..2081ff3 --- /dev/null +++ b/lens/compound/join-string.rkt @@ -0,0 +1,34 @@ +#lang racket/base + +(require racket/contract + "../base/main.rkt" + "../util/immutable.rkt" + "compose.rkt" + "inverse-function-lens.rkt" + "join-list.rkt") + +(module+ test + (require rackunit + "../list/list-ref-take-drop.rkt")) + +(provide + (contract-out + [lens-join/string (->* () #:rest (listof lens?) (lens/c any/c immutable-string?))])) + + +(define (lens-join/string . lenses) + (lens-compose list->string-lens (apply lens-join/list lenses))) + +(define list->string-lens + (inverse-function-lens list->immutable-string string->list)) + +(module+ test + (define string-first-third-fifth-lens + (lens-join/string first-lens + third-lens + fifth-lens)) + (check-equal? (lens-view string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f)) + "ace") + (check-pred immutable? (lens-view string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f))) + (check-equal? (lens-set string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f) "ACE") + '(#\A #\b #\C #\d #\E #\f))) diff --git a/lens/compound/join-vector.rkt b/lens/compound/join-vector.rkt new file mode 100644 index 0000000..01bf200 --- /dev/null +++ b/lens/compound/join-vector.rkt @@ -0,0 +1,35 @@ +#lang racket/base + +(require racket/contract + "../base/main.rkt" + "../util/immutable.rkt" + "compose.rkt" + "inverse-function-lens.rkt" + "join-list.rkt") + +(module+ test + (require rackunit + "../list/list-ref-take-drop.rkt")) + +(provide + (contract-out + [lens-join/vector (->* () #:rest (listof lens?) (lens/c any/c immutable-vector?))])) + + +(define (lens-join/vector . lenses) + (lens-compose list->vector-lens (apply lens-join/list lenses))) + +(define list->vector-lens + (inverse-function-lens list->immutable-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-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/lens/join.scrbl b/lens/compound/join.scrbl similarity index 100% rename from lens/join.scrbl rename to lens/compound/join.scrbl diff --git a/lens/compound/main.rkt b/lens/compound/main.rkt new file mode 100644 index 0000000..b82bc06 --- /dev/null +++ b/lens/compound/main.rkt @@ -0,0 +1,16 @@ +#lang racket/base + +(require "compose.rkt" + "join-hash.rkt" + "join-list.rkt" + "join-string.rkt" + "join-vector.rkt" + "thrush.rkt") + +(provide (all-from-out + "compose.rkt" + "join-hash.rkt" + "join-list.rkt" + "join-string.rkt" + "join-vector.rkt" + "thrush.rkt")) diff --git a/lens/compound/thrush.rkt b/lens/compound/thrush.rkt new file mode 100644 index 0000000..3a2d176 --- /dev/null +++ b/lens/compound/thrush.rkt @@ -0,0 +1,30 @@ +#lang racket/base + +(require racket/contract + racket/list + fancy-app + "../base/main.rkt" + "compose.rkt") + +(module+ test + (require rackunit + "../list/list-ref-take-drop.rkt")) + +(provide + (contract-out [lens-thrush (->* () () #:rest (listof lens?) lens?)])) + + +(define (lens-thrush . args) + (apply lens-compose (reverse args))) + +(module+ test + (define (set-first l v) + (list* v (rest l))) + (define first-lens (make-lens first set-first)) + (define (set-second l v) + (list* (first l) v (rest (rest l)))) + (define second-lens (make-lens second set-second)) + (define test-alist '((a 1) (b 2) (c 3))) + (define first-of-second-lens (lens-thrush second-lens first-lens)) + (check-equal? (lens-view first-of-second-lens test-alist) 'b) + (check-equal? (lens-set first-of-second-lens test-alist 'B) '((a 1) (B 2) (c 3)))) diff --git a/lens/hash-pick.rkt b/lens/hash-pick.rkt deleted file mode 100644 index 820cd0c..0000000 --- a/lens/hash-pick.rkt +++ /dev/null @@ -1,30 +0,0 @@ -#lang racket/base - -(require racket/contract/base) -(provide (contract-out - [hash-pick-lens - (->* [] #:rest list? (lens/c immutable-hash? immutable-hash?))] - )) - -(require racket/list - "base/main.rkt" - "util/immutable.rkt" - "hash.rkt" - "join.rkt") - -(module+ test - (require rackunit)) - -(define (hash-pick-lens . ks) - (apply lens-join/hash - (append-map - (λ (k) - (list k (hash-ref-lens k))) - ks))) - -(module+ test - (check-equal? (lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3)) - (hash 'a 1 'c 3)) - (check-equal? (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5)) - (hash 'a 4 'b 2 'c 5)) - ) diff --git a/lens/hash.rkt b/lens/hash/hash.rkt similarity index 78% rename from lens/hash.rkt rename to lens/hash/hash.rkt index 7aff9e6..9f26690 100644 --- a/lens/hash.rkt +++ b/lens/hash/hash.rkt @@ -1,16 +1,19 @@ -#lang racket +#lang racket/base + +(require racket/contract + fancy-app + "../base/main.rkt" + "../compound/main.rkt" + "../util/immutable.rkt" + "../util/rest-contract.rkt") + +(module+ test + (require rackunit)) (provide (contract-out [hash-ref-lens (-> any/c (lens/c immutable-hash? any/c))] - [hash-ref-nested-lens (->* () #:rest list? (lens/c immutable-hash? any/c))])) - -(require fancy-app - "base/main.rkt" - "util/immutable.rkt") - -(module+ test - (require rackunit)) + [hash-ref-nested-lens (rest-> any/c (lens/c immutable-hash? any/c))])) (define (hash-ref-lens key) diff --git a/lens/hash.scrbl b/lens/hash/hash.scrbl similarity index 100% rename from lens/hash.scrbl rename to lens/hash/hash.scrbl diff --git a/lens/hash/main.rkt b/lens/hash/main.rkt new file mode 100644 index 0000000..9194492 --- /dev/null +++ b/lens/hash/main.rkt @@ -0,0 +1,10 @@ +#lang racket/base + + +(require "hash.rkt" + "pick.rkt") + + +(provide + (all-from-out "hash.rkt" + "pick.rkt")) diff --git a/lens/hash/pick.rkt b/lens/hash/pick.rkt new file mode 100644 index 0000000..f0cd441 --- /dev/null +++ b/lens/hash/pick.rkt @@ -0,0 +1,30 @@ +#lang racket/base + +(require racket/contract + racket/list + "../base/main.rkt" + "../compound/join-hash.rkt" + "../util/immutable.rkt" + "../util/rest-contract.rkt" + "hash.rkt") + +(module+ test + (require rackunit)) + +(provide + (contract-out + [hash-pick-lens (rest-> any/c (lens/c immutable-hash? immutable-hash?))])) + + +(define (hash-ref-lens-and-key k) + (list k (hash-ref-lens k))) + +(define (hash-pick-lens . ks) + (apply lens-join/hash + (append-map hash-ref-lens-and-key ks))) + +(module+ test + (check-equal? (lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3)) + (hash 'a 1 'c 3)) + (check-equal? (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5)) + (hash 'a 4 'b 2 'c 5))) diff --git a/lens/hash-pick.scrbl b/lens/hash/pick.scrbl similarity index 100% rename from lens/hash-pick.scrbl rename to lens/hash/pick.scrbl diff --git a/lens/join.rkt b/lens/join.rkt deleted file mode 100644 index 906c090..0000000 --- a/lens/join.rkt +++ /dev/null @@ -1,118 +0,0 @@ -#lang racket - -(require fancy-app - "base/main.rkt" - "list/main.rkt" - "util/list-pair-contract.rkt" - "util/immutable.rkt" - unstable/sequence) - -(module+ test - (require rackunit)) - -(provide - (contract-out - [lens-join/list (->* () #:rest (listof lens?) (lens/c any/c list?))] - [lens-join/hash (->* () #:rest (listof2 any/c lens?) (lens/c any/c immutable-hash?))] - [lens-join/vector (->* () #:rest (listof lens?) (lens/c any/c immutable-vector?))] - [lens-join/string (->* () #:rest (listof lens?) (lens/c any/c immutable-string?))] - )) - - -(define (zip xs ys) - (append-map list xs ys)) - -(define (lens-join/list . lenses) - (define (get target) - (apply lens-view/list target lenses)) - (define (set target new-views) - (apply lens-set/list target (zip lenses new-views))) - (make-lens get set)) - - -(module+ test - (define first-third-fifth-lens - (lens-join/list first-lens - third-lens - fifth-lens)) - (check-equal? (lens-view first-third-fifth-lens '(a b c d e f)) - '(a c e)) - (check-equal? (lens-set first-third-fifth-lens '(a b c d e f) '(1 2 3)) - '(1 b 2 d 3 f))) -(define first-first-lens - (lens-join/list first-lens - first-lens)) - - -(define (value-list->hash keys vs) - (make-immutable-hash (map cons keys vs))) - -(define (split-slice n vs) - (define grouped - (for/list ([group (in-slice n vs)]) - group)) - (define (get-ith i) - (map (list-ref _ i) grouped)) - (build-list n get-ith)) - -(module+ test - (check-equal? (split-slice 3 '(a 1 FOO b 2 BAR c 3 BAZ)) - '((a b c) (1 2 3) (FOO BAR BAZ)))) - - -(define (lens-join/hash . keys/lenses) - (match-define (list keys lenses) (split-slice 2 keys/lenses)) - (define list-lens (apply lens-join/list lenses)) - (define (get target) - (value-list->hash keys (lens-view list-lens target))) - (define (set target new-view-hash) - (lens-set list-lens target (map (hash-ref new-view-hash _) keys))) - (make-lens get set)) - -(module+ test - (define a-b-lens (lens-join/hash 'b third-lens - 'a first-lens)) - (check-equal? (lens-view a-b-lens '(1 2 3)) - (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->immutable-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-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))) - -(define (lens-join/string . lenses) - (lens-compose list->string-lens (apply lens-join/list lenses))) - -(define list->string-lens - (inverse-function-lens list->immutable-string string->list)) - -(module+ test - (define string-first-third-fifth-lens - (lens-join/string first-lens - third-lens - fifth-lens)) - (check-equal? (lens-view string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f)) - "ace") - (check-pred immutable? (lens-view string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f))) - (check-equal? (lens-set string-first-third-fifth-lens '(#\a #\b #\c #\d #\e #\f) "ACE") - '(#\A #\b #\C #\d #\E #\f))) diff --git a/lens/list/cadr-etc.rkt b/lens/list/cadr-etc.rkt index 76b5b3f..04a8d44 100644 --- a/lens/list/cadr-etc.rkt +++ b/lens/list/cadr-etc.rkt @@ -1,9 +1,12 @@ -#lang racket +#lang racket/base -(require syntax/parse/define +(require racket/contract + syntax/parse/define "../base/main.rkt" + "../compound/main.rkt" "car-cdr.rkt" - (for-syntax racket/syntax)) + (for-syntax racket/base + racket/syntax)) (module+ test (require rackunit)) diff --git a/lens/list/list-ref-take-drop.rkt b/lens/list/list-ref-take-drop.rkt index b01500e..73f66eb 100644 --- a/lens/list/list-ref-take-drop.rkt +++ b/lens/list/list-ref-take-drop.rkt @@ -26,6 +26,7 @@ fancy-app "../util/improper-list-length.rkt" "../base/main.rkt" + "../compound/compose.rkt" "car-cdr.rkt") (module+ test diff --git a/lens/list/main.rkt b/lens/list/main.rkt index 37bacce..a7fc4fc 100644 --- a/lens/list/main.rkt +++ b/lens/list/main.rkt @@ -3,10 +3,12 @@ (require "car-cdr.rkt" "list-ref-take-drop.rkt" "cadr-etc.rkt" + "multi.rkt" "assoc.rkt") (provide (all-from-out "car-cdr.rkt" "list-ref-take-drop.rkt" "cadr-etc.rkt" + "multi.rkt" "assoc.rkt")) diff --git a/lens/list.rkt b/lens/list/multi.rkt similarity index 65% rename from lens/list.rkt rename to lens/list/multi.rkt index ef11993..980714a 100644 --- a/lens/list.rkt +++ b/lens/list/multi.rkt @@ -1,21 +1,18 @@ -#lang racket +#lang racket/base -(require "base/main.rkt" - "list/main.rkt" - "join.rkt") +(require racket/contract + "../base/main.rkt" + "../compound/main.rkt" + "../util/rest-contract.rkt" + "list-ref-take-drop.rkt") (module+ test (require rackunit)) (provide - (contract-out - [list-ref-nested-lens - (->* () #:rest (listof exact-nonnegative-integer?) - lens?)] - [list-refs-lens - (->* () #:rest (listof exact-nonnegative-integer?) - (lens/c list? list?))] - )) + (contract-out + [list-ref-nested-lens (rest-> exact-nonnegative-integer? lens?)] + [list-refs-lens (rest-> exact-nonnegative-integer? (lens/c list? list?))])) (define (list-ref-nested-lens . indices) diff --git a/lens/list.scrbl b/lens/list/multi.scrbl similarity index 100% rename from lens/list.scrbl rename to lens/list/multi.scrbl diff --git a/lens/main.rkt b/lens/main.rkt index 2820ee4..92fa4f5 100644 --- a/lens/main.rkt +++ b/lens/main.rkt @@ -1,22 +1,28 @@ -#lang racket +#lang racket/base (require "base/main.rkt" + "compound/main.rkt" + "hash/main.rkt" "list/main.rkt" - "struct.rkt" + "struct/main.rkt" "dict.rkt" - "hash.rkt" - "stream.rkt") + "stream.rkt" + "string.rkt" + "vector/main.rkt") (provide (except-out (all-from-out "base/main.rkt" + "compound/main.rkt" + "hash/main.rkt" "list/main.rkt" - "struct.rkt" + "struct/main.rkt" + "vector/main.rkt" "dict.rkt" - "hash.rkt" - "stream.rkt") + "stream.rkt" + "string.rkt") focus-lens drop-lens take-lens diff --git a/lens/stream.rkt b/lens/stream.rkt index 6f412a8..882e7c4 100644 --- a/lens/stream.rkt +++ b/lens/stream.rkt @@ -6,7 +6,8 @@ (require racket/stream fancy-app - "base/main.rkt") + "base/main.rkt" + "compound/main.rkt") (module+ test (require rackunit "test-util/test-lens.rkt")) diff --git a/lens/string.rkt b/lens/string.rkt index 3fbe370..c0be661 100644 --- a/lens/string.rkt +++ b/lens/string.rkt @@ -13,7 +13,7 @@ (require fancy-app "base/main.rkt" "util/immutable.rkt" - "join.rkt") + "compound/main.rkt") (module+ test (require rackunit)) diff --git a/lens/struct.rkt b/lens/struct/field.rkt similarity index 91% rename from lens/struct.rkt rename to lens/struct/field.rkt index f0ec22e..ec4285f 100644 --- a/lens/struct.rkt +++ b/lens/struct/field.rkt @@ -1,18 +1,19 @@ #lang racket/base -(provide struct-lens) - (require racket/local syntax/parse/define alexis/util/struct - "base/main.rkt" + "../base/main.rkt" (for-syntax racket/base syntax/parse - racket/syntax - )) + racket/syntax)) + (module+ test (require rackunit fancy-app)) +(provide struct-lens) + + (define-simple-macro (struct-lens s:id fld:id) #:with s-fld (format-id #'s "~a-~a" #'s #'fld #:source #'fld) #:with s-fld-set (format-id #'s "~a-~a-set" #'s #'fld #:source #'fld) diff --git a/lens/struct.scrbl b/lens/struct/field.scrbl similarity index 100% rename from lens/struct.scrbl rename to lens/struct/field.scrbl diff --git a/lens/struct/main.rkt b/lens/struct/main.rkt new file mode 100644 index 0000000..3912407 --- /dev/null +++ b/lens/struct/main.rkt @@ -0,0 +1,8 @@ +#lang racket/base + +(require "field.rkt" + "struct.rkt") + +(provide + (all-from-out "field.rkt" + "struct.rkt")) diff --git a/unstable/lens/struct.rkt b/lens/struct/struct.rkt similarity index 94% rename from unstable/lens/struct.rkt rename to lens/struct/struct.rkt index 93e00f4..ee9d694 100644 --- a/unstable/lens/struct.rkt +++ b/lens/struct/struct.rkt @@ -1,8 +1,5 @@ #lang racket/base -(provide define-struct-lenses - struct/lens) - (require syntax/parse/define lens/base/main alexis/util/struct @@ -10,12 +7,15 @@ (for-syntax racket/base syntax/parse racket/syntax - racket/struct-info - )) + racket/struct-info)) + (module+ test (require rackunit fancy-app - lens/test-util/test-lens)) + "../test-util/test-lens.rkt")) + +(provide define-struct-lenses + struct/lens) (define-for-syntax (get-struct-field-ids struct-info failure-context) diff --git a/unstable/lens/struct.scrbl b/lens/struct/struct.scrbl similarity index 100% rename from unstable/lens/struct.scrbl rename to lens/struct/struct.scrbl diff --git a/lens/test-util/test-lens.rkt b/lens/test-util/test-lens.rkt index def9054..42608b0 100644 --- a/lens/test-util/test-lens.rkt +++ b/lens/test-util/test-lens.rkt @@ -1,6 +1,7 @@ -#lang racket +#lang racket/base -(require rackunit +(require racket/contract + rackunit fancy-app "../base/base.rkt" "../base/view-set.rkt") diff --git a/lens/tests/doc-coverage.rkt b/lens/tests/doc-coverage.rkt index b7d1f0b..28aeb05 100644 --- a/lens/tests/doc-coverage.rkt +++ b/lens/tests/doc-coverage.rkt @@ -1,4 +1,4 @@ -#lang racket +#lang racket/base (require doc-coverage lens) diff --git a/lens/util/list-pair-contract.rkt b/lens/util/list-pair-contract.rkt index 411a3e3..649bc88 100644 --- a/lens/util/list-pair-contract.rkt +++ b/lens/util/list-pair-contract.rkt @@ -1,10 +1,26 @@ -#lang racket +#lang racket/base + +(require racket/contract + racket/list + racket/match) (provide (contract-out [listof2 (-> contract? contract? contract?)])) -(define (listof2 first-val/c second-val/c) +(define (list*/c . contracts) + (match contracts + [(list end-contract) + end-contract] + [(list* head-contract rest-contracts) + (cons/c head-contract + (apply list*/c rest-contracts))])) + +(define (repeating-list/c . contracts) (define c - (or/c empty? (cons/c first-val/c (cons/c second-val/c (recursive-contract c))))) + (or/c empty? + (apply list*/c (append contracts (list (recursive-contract c)))))) c) + +(define (listof2 first-val/c second-val/c) + (repeating-list/c first-val/c second-val/c)) diff --git a/lens/util/rest-contract.rkt b/lens/util/rest-contract.rkt new file mode 100644 index 0000000..2d7985e --- /dev/null +++ b/lens/util/rest-contract.rkt @@ -0,0 +1,11 @@ +#lang racket/base + +(require racket/contract) + +(provide + (contract-out + [rest-> (-> contract? contract? contract?)])) + + +(define (rest-> arg-contract result-contract) + (->* () #:rest (listof arg-contract) result-contract)) diff --git a/lens/vector.rkt b/lens/vector.rkt deleted file mode 100644 index 2fb8b8f..0000000 --- a/lens/vector.rkt +++ /dev/null @@ -1,62 +0,0 @@ -#lang racket/base - -(require racket/contract/base) -(provide (contract-out - [vector-ref-lens - (-> exact-nonnegative-integer? - (lens/c immutable-vector? any/c))] - [vector-ref-nested-lens - (->* [] #:rest (listof exact-nonnegative-integer?) - (lens/c immutable-vector? any/c))] - [vector-pick-lens - (->* [] #:rest (listof exact-nonnegative-integer?) - (lens/c immutable-vector? immutable-vector?))] - )) - -(require fancy-app - "base/main.rkt" - "list/main.rkt" - "util/immutable.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) - (build-immutable-vector - (vector-length v) - (λ (j) - (if (= i j) - x - (vector-ref v j))))) - -(module+ test - (check-equal? (lens-view (vector-ref-lens 0) #(a b c)) 'a) - (check-equal? (lens-set (vector-ref-lens 2) #(a b c) "C") #(a b "C"))) - - -(define (vector-ref-nested-lens . is) - (apply lens-thrush (map vector-ref-lens is))) - -(module+ test - (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 (vector-pick-lens . is) - (apply lens-join/vector (map vector-ref-lens is))) - -(module+ test - (define 1-5-6-lens (vector-pick-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/lens/vector/main.rkt b/lens/vector/main.rkt new file mode 100644 index 0000000..24d3654 --- /dev/null +++ b/lens/vector/main.rkt @@ -0,0 +1,12 @@ +#lang racket/base + + +(require "nested.rkt" + "pick.rkt" + "ref.rkt") + + +(provide + (all-from-out "nested.rkt" + "pick.rkt" + "ref.rkt")) diff --git a/lens/vector/nested.rkt b/lens/vector/nested.rkt new file mode 100644 index 0000000..1a610e8 --- /dev/null +++ b/lens/vector/nested.rkt @@ -0,0 +1,27 @@ +#lang racket/base + +(require racket/contract + "../base/main.rkt" + "../compound/main.rkt" + "../util/immutable.rkt" + "../util/rest-contract.rkt" + "ref.rkt") + +(module+ test + (require rackunit)) + +(provide + (contract-out + [vector-ref-nested-lens (rest-> exact-nonnegative-integer? + (lens/c immutable-vector? any/c))])) + + +(define (vector-ref-nested-lens . is) + (apply lens-thrush (map vector-ref-lens is))) + +(module+ test + (check-equal? (lens-transform (vector-ref-nested-lens 2 1) + #(a #(b c) #(d e f)) + symbol->string) + #(a #(b c) #(d "e" f)))) + diff --git a/lens/vector/pick.rkt b/lens/vector/pick.rkt new file mode 100644 index 0000000..85252dd --- /dev/null +++ b/lens/vector/pick.rkt @@ -0,0 +1,27 @@ +#lang racket/base + +(require racket/contract + "../base/main.rkt" + "../compound/main.rkt" + "../util/immutable.rkt" + "../util/rest-contract.rkt" + "ref.rkt") + +(module+ test + (require rackunit)) + +(provide + (contract-out + [vector-pick-lens (rest-> exact-nonnegative-integer? + (lens/c immutable-vector? immutable-vector?))])) + + +(define (vector-pick-lens . is) + (apply lens-join/vector (map vector-ref-lens is))) + +(module+ test + (define 1-5-6-lens (vector-pick-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/lens/vector/ref.rkt b/lens/vector/ref.rkt new file mode 100644 index 0000000..d2dbf7e --- /dev/null +++ b/lens/vector/ref.rkt @@ -0,0 +1,32 @@ +#lang racket/base + +(require racket/contract + fancy-app + "../base/main.rkt" + "../util/immutable.rkt") + +(module+ test + (require rackunit)) + +(provide + (contract-out + [vector-ref-lens (-> exact-nonnegative-integer? + (lens/c immutable-vector? any/c))])) + + +(define (vector-ref-lens i) + (make-lens + (vector-ref _ i) + (vector-set _ i _))) + +(define (vector-set v i x) + (build-immutable-vector + (vector-length v) + (λ (j) + (if (= i j) + x + (vector-ref v j))))) + +(module+ test + (check-equal? (lens-view (vector-ref-lens 0) #(a b c)) 'a) + (check-equal? (lens-set (vector-ref-lens 2) #(a b c) "C") #(a b "C"))) diff --git a/lens/vector.scrbl b/lens/vector/ref.scrbl similarity index 100% rename from lens/vector.scrbl rename to lens/vector/ref.scrbl