From 9e6700b9a3b20b59f18b8a7c2688042eea5d1d5a Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Fri, 10 Jul 2015 00:22:37 -0700 Subject: [PATCH] Add ref nested list and hash lenses --- info.rkt | 2 +- lens/list/list-ref-take-drop.rkt | 8 +------- lens/main.rkt | 1 - unstable/lens/compound.scrbl | 1 + {lens => unstable/lens}/hash.rkt | 13 +++++++------ unstable/lens/hash.scrbl | 32 ++++++++++++++++++++++++++++++++ unstable/lens/list.rkt | 17 +++++++++++++++++ unstable/lens/list.scrbl | 21 +++++++++++++++++++++ unstable/lens/main.rkt | 8 ++++++-- unstable/lens/main.scrbl | 2 ++ 10 files changed, 88 insertions(+), 17 deletions(-) rename {lens => unstable/lens}/hash.rkt (74%) create mode 100644 unstable/lens/hash.scrbl create mode 100644 unstable/lens/list.rkt create mode 100644 unstable/lens/list.scrbl diff --git a/info.rkt b/info.rkt index c6683a6..d76db69 100644 --- a/info.rkt +++ b/info.rkt @@ -3,7 +3,7 @@ (define collection 'multi) -(define version "1.0") +(define version "1.1") (define deps diff --git a/lens/list/list-ref-take-drop.rkt b/lens/list/list-ref-take-drop.rkt index 775a886..1a81b0a 100644 --- a/lens/list/list-ref-take-drop.rkt +++ b/lens/list/list-ref-take-drop.rkt @@ -3,7 +3,6 @@ (provide (contract-out [list-ref-lens (-> exact-nonnegative-integer? lens?)] - [list-ref-nested-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)] [take-lens (-> exact-nonnegative-integer? lens?)] [drop-lens (-> exact-nonnegative-integer? lens?)] [first-lens lens?] @@ -61,9 +60,6 @@ (define (list-ref-lens i) (lens-compose car-lens (drop-lens i))) -(define (list-ref-nested-lens . args) - (apply lens-thrush (map list-ref-lens args))) - (define first-lens (list-ref-lens 0)) (define second-lens (list-ref-lens 1)) (define third-lens (list-ref-lens 2)) @@ -86,6 +82,4 @@ (check-equal? (lens-set second-lens '(1 2 3 4 5) 'a) '(1 a 3 4 5)) (check-equal? (lens-set third-lens '(1 2 3 4 5) 'a) '(1 2 a 4 5)) (check-equal? (lens-set fourth-lens '(1 2 3 4 5) 'a) '(1 2 3 a 5)) - (check-equal? (lens-set fifth-lens '(1 2 3 4 5) 'a) '(1 2 3 4 a)) - (check-equal? (lens-transform/list '(a (b c) (d e f)) (list-ref-nested-lens 2 1) symbol->string) - '(a (b c) (d "e" f)))) + (check-equal? (lens-set fifth-lens '(1 2 3 4 5) 'a) '(1 2 3 4 a))) diff --git a/lens/main.rkt b/lens/main.rkt index a7f3ae4..066e6f8 100644 --- a/lens/main.rkt +++ b/lens/main.rkt @@ -17,6 +17,5 @@ ) focus-lens drop-lens - list-ref-nested-lens take-lens use-applicable-lenses!)) diff --git a/unstable/lens/compound.scrbl b/unstable/lens/compound.scrbl index 71d4cd0..c12cf10 100644 --- a/unstable/lens/compound.scrbl +++ b/unstable/lens/compound.scrbl @@ -3,6 +3,7 @@ @(require scribble/eval lens/lenses-examples (for-label lens + unstable/lens racket/base racket/contract)) diff --git a/lens/hash.rkt b/unstable/lens/hash.rkt similarity index 74% rename from lens/hash.rkt rename to unstable/lens/hash.rkt index 3d18c31..a830e5f 100644 --- a/lens/hash.rkt +++ b/unstable/lens/hash.rkt @@ -2,25 +2,26 @@ (provide (contract-out - [hash-ref-lens (-> any/c lens?)])) + [hash-ref-lens (-> any/c lens?)] + [hash-ref-nested-lens (->* () #:rest list? lens?)])) (require fancy-app - "base/main.rkt") + lens) (module+ test (require rackunit)) -(define (hash-ref-lens1 key) +(define (hash-ref-lens key) (make-lens (hash-ref _ key) (hash-set _ key _))) -(define (hash-ref-lens . keys) - (apply lens-thrush (map hash-ref-lens1 keys))) +(define (hash-ref-nested-lens . keys) + (apply lens-thrush (map hash-ref-lens keys))) (module+ test (define a (hash-ref-lens 'a)) - (define a-x (hash-ref-lens 'a 'x)) + (define a-x (hash-ref-nested-lens 'a 'x)) (let-lens [val ctxt] a (hash 'a 1 'b 2 'c 3) (check-equal? val 1) (check-equal? (ctxt 100) (hash 'a 100 'b 2 'c 3))) diff --git a/unstable/lens/hash.scrbl b/unstable/lens/hash.scrbl new file mode 100644 index 0000000..0aa7f26 --- /dev/null +++ b/unstable/lens/hash.scrbl @@ -0,0 +1,32 @@ +#lang scribble/manual + +@(require scribble/eval + lens/lenses-examples + (for-label lens + unstable/lens + racket/base + racket/contract)) + + +@title{Hash Lenses} + +@defmodule[unstable/lens/hash] + +@defproc[(hash-ref-lens [key any/c]) lens?]{ + Constructs a lens that targets hashes and views the value + of @racket[key]. + @lenses-unstable-examples[ + (define foo-lens (hash-ref-lens 'foo)) + (lens-view foo-lens (hash 'foo 10 'bar 20)) + (lens-set foo-lens (hash 'foo 10 'bar 20) 1000) +]} + +@defproc[(hash-ref-nested-lens [key any/c] ...) lens?]{ + Contructs a lens that targets hashes with nested hashes + as values and views the value obtained by using each + @racket[key] in order. + @lenses-unstable-examples[ + (define foo-bar-lens (hash-ref-nested-lens 'foo 'bar)) + (lens-view foo-bar-lens (hash 'foo (hash 'bar 1))) + (lens-set foo-bar-lens (hash 'foo (hash 'bar 1)) 1000) +]} diff --git a/unstable/lens/list.rkt b/unstable/lens/list.rkt new file mode 100644 index 0000000..36b2747 --- /dev/null +++ b/unstable/lens/list.rkt @@ -0,0 +1,17 @@ +#lang racket + +(require lens) + +(module+ test + (require rackunit)) + +(provide + (contract-out + [list-ref-nested-lens (->* () #:rest (listof exact-nonnegative-integer?) lens?)])) + +(define (list-ref-nested-lens . args) + (apply lens-thrush (map list-ref-lens args))) + +(module+ test + (check-equal? (lens-transform/list '(a (b c) (d e f)) (list-ref-nested-lens 2 1) symbol->string) + '(a (b c) (d "e" f)))) diff --git a/unstable/lens/list.scrbl b/unstable/lens/list.scrbl new file mode 100644 index 0000000..34c0942 --- /dev/null +++ b/unstable/lens/list.scrbl @@ -0,0 +1,21 @@ +#lang scribble/manual + +@(require scribble/eval + lens/lenses-examples + (for-label lens + racket/base + racket/contract)) + + +@title{List Lenses} + +@defmodule[unstable/lens/list] + +@defproc[(list-ref-nested-lens [index exact-nonnegative-integer?] ...) lens?]{ + Constructs a lens that views into a tree made from nested lists. + Indexing starts from zero in the same was as @racket[list-ref-lens]. + @lenses-unstable-examples[ + (define first-of-second-lens (list-ref-nested-lens 1 0)) + (lens-view first-of-second-lens '(1 (a b c) 2 3)) + (lens-set first-of-second-lens '(1 (a b c) 2 3) 'foo) +]} diff --git a/unstable/lens/main.rkt b/unstable/lens/main.rkt index d488efe..70d8cf1 100644 --- a/unstable/lens/main.rkt +++ b/unstable/lens/main.rkt @@ -1,7 +1,11 @@ #lang racket (require "syntax.rkt" - "compound.rkt") + "compound.rkt" + "list.rkt" + "hash.rkt") (provide (all-from-out "syntax.rkt" - "compound.rkt")) + "compound.rkt" + "list.rkt" + "hash.rkt")) diff --git a/unstable/lens/main.scrbl b/unstable/lens/main.scrbl index 8085b8b..665bb09 100644 --- a/unstable/lens/main.scrbl +++ b/unstable/lens/main.scrbl @@ -10,4 +10,6 @@ may change in future releases. Do not depend on this library being backwards-compatible. @include-section["compound.scrbl"] +@include-section["list.scrbl"] +@include-section["hash.scrbl"] @include-section["syntax.scrbl"]