diff --git a/lens/hash/main.rkt b/lens/hash/main.rkt index 9194492..24d3654 100644 --- a/lens/hash/main.rkt +++ b/lens/hash/main.rkt @@ -1,10 +1,12 @@ #lang racket/base -(require "hash.rkt" - "pick.rkt") +(require "nested.rkt" + "pick.rkt" + "ref.rkt") (provide - (all-from-out "hash.rkt" - "pick.rkt")) + (all-from-out "nested.rkt" + "pick.rkt" + "ref.rkt")) diff --git a/lens/hash/main.scrbl b/lens/hash/main.scrbl new file mode 100644 index 0000000..8b628a0 --- /dev/null +++ b/lens/hash/main.scrbl @@ -0,0 +1,7 @@ +#lang scribble/manual + +@title{Hash Lenses} + +@include-section["ref.scrbl"] +@include-section["nested.scrbl"] +@include-section["pick.scrbl"] diff --git a/lens/hash/hash.rkt b/lens/hash/nested.rkt similarity index 53% rename from lens/hash/hash.rkt rename to lens/hash/nested.rkt index 9f26690..5a89596 100644 --- a/lens/hash/hash.rkt +++ b/lens/hash/nested.rkt @@ -1,36 +1,25 @@ -#lang racket/base +#lang racket -(require racket/contract - fancy-app - "../base/main.rkt" +(require "../base/main.rkt" "../compound/main.rkt" "../util/immutable.rkt" - "../util/rest-contract.rkt") + "../util/rest-contract.rkt" + "ref.rkt") (module+ test - (require rackunit)) + (require rackunit + fancy-app)) (provide (contract-out - [hash-ref-lens (-> any/c (lens/c immutable-hash? any/c))] [hash-ref-nested-lens (rest-> any/c (lens/c immutable-hash? any/c))])) -(define (hash-ref-lens key) - (make-lens (hash-ref _ key) - (hash-set _ key _))) - (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-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))) - (check-equal? (lens-transform/list (hash 'a 1 'b 2 'c 3) a (* 10 _)) - (hash 'a 10 'b 2 'c 3)) (let-lens [val ctxt] a-x (hash 'a (hash 'x 1 'y 2) 'b (hash 'z 3)) (check-equal? val 1) (check-equal? (ctxt 100) (hash 'a (hash 'x 100 'y 2) 'b (hash 'z 3)))) diff --git a/lens/hash/hash.scrbl b/lens/hash/nested.scrbl similarity index 54% rename from lens/hash/hash.scrbl rename to lens/hash/nested.scrbl index 198f307..d85c45e 100644 --- a/lens/hash/hash.scrbl +++ b/lens/hash/nested.scrbl @@ -1,19 +1,8 @@ #lang scribble/manual -@(require "doc-util/main.rkt") +@(require "../doc-util/main.rkt") -@title{Hash Lenses} - -@defproc[(hash-ref-lens [key any/c]) lens?]{ - Constructs a lens that targets hashes and views the value - of @racket[key]. - @lenses-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 diff --git a/lens/hash/pick.rkt b/lens/hash/pick.rkt index f0cd441..8ebe774 100644 --- a/lens/hash/pick.rkt +++ b/lens/hash/pick.rkt @@ -6,7 +6,7 @@ "../compound/join-hash.rkt" "../util/immutable.rkt" "../util/rest-contract.rkt" - "hash.rkt") + "ref.rkt") (module+ test (require rackunit)) diff --git a/lens/hash/pick.scrbl b/lens/hash/pick.scrbl index 92c066a..05d96a6 100644 --- a/lens/hash/pick.scrbl +++ b/lens/hash/pick.scrbl @@ -1,14 +1,13 @@ #lang scribble/manual -@(require "doc-util/main.rkt") +@(require "../doc-util/main.rkt") -@title{Viewing a subset of a hash table by key} @defproc[(hash-pick-lens [key any/c] ...) lens?]{ -Creates a lens that views a subset of the target hash-table with the given -@racket[key]s. The view, is another hash-table with only the given keys and -their corrosponding values in the target hash-table. -@lenses-examples[ - (lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3)) - (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5)) + Creates a lens that views a subset of the target hash-table with the given + @racket[key]s. The view, is another hash-table with only the given keys and + their corrosponding values in the target hash-table. + @lenses-examples[ + (lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3)) + (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5)) ]} diff --git a/lens/hash/ref.rkt b/lens/hash/ref.rkt new file mode 100644 index 0000000..1beeee7 --- /dev/null +++ b/lens/hash/ref.rkt @@ -0,0 +1,26 @@ +#lang racket/base + +(require racket/contract + fancy-app + "../base/main.rkt" + "../util/immutable.rkt") + +(module+ test + (require rackunit)) + +(provide + (contract-out + [hash-ref-lens (-> any/c (lens/c immutable-hash? any/c))])) + + +(define (hash-ref-lens key) + (make-lens (hash-ref _ key) + (hash-set _ key _))) + +(module+ test + (define a (hash-ref-lens 'a)) + (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))) + (check-equal? (lens-transform/list (hash 'a 1 'b 2 'c 3) a (* 10 _)) + (hash 'a 10 'b 2 'c 3))) diff --git a/lens/hash/ref.scrbl b/lens/hash/ref.scrbl new file mode 100644 index 0000000..0477fd9 --- /dev/null +++ b/lens/hash/ref.scrbl @@ -0,0 +1,13 @@ +#lang scribble/manual + +@(require "../doc-util/main.rkt") + + +@defproc[(hash-ref-lens [key any/c]) lens?]{ + Constructs a lens that targets hashes and views the value + of @racket[key]. + @lenses-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) +]} diff --git a/lens/main.rkt b/lens/main.rkt index 92fa4f5..6053f9f 100644 --- a/lens/main.rkt +++ b/lens/main.rkt @@ -3,12 +3,12 @@ (require "base/main.rkt" "compound/main.rkt" + "dict.rkt" "hash/main.rkt" "list/main.rkt" - "struct/main.rkt" - "dict.rkt" "stream.rkt" "string.rkt" + "struct/main.rkt" "vector/main.rkt") (provide @@ -16,13 +16,13 @@ (all-from-out "base/main.rkt" "compound/main.rkt" + "dict.rkt" "hash/main.rkt" "list/main.rkt" - "struct/main.rkt" - "vector/main.rkt" - "dict.rkt" "stream.rkt" - "string.rkt") + "string.rkt" + "struct/main.rkt" + "vector/main.rkt") focus-lens drop-lens take-lens diff --git a/lens/struct/struct.scrbl b/lens/struct/struct.scrbl index 868db56..b1091be 100644 --- a/lens/struct/struct.scrbl +++ b/lens/struct/struct.scrbl @@ -2,21 +2,20 @@ @(require "../doc-util/main.rkt") -@defmodule[unstable/lens/struct] @defform[(define-struct-lenses struct-id)]{ -Given a @racket[struct-id], defines a lens for each of its fields. -@lenses-unstable-examples[ - (struct foo (a b c) #:transparent) - (define-struct-lenses foo) - (lens-view foo-a-lens (foo 1 2 3)) - (lens-set foo-a-lens (foo 1 2 3) 100) + Given a @racket[struct-id], defines a lens for each of its fields. + @lenses-examples[ + (struct foo (a b c) #:transparent) + (define-struct-lenses foo) + (lens-view foo-a-lens (foo 1 2 3)) + (lens-set foo-a-lens (foo 1 2 3) 100) ]} @defform[(struct/lens struct-id (field-spec ...) struct-option ...)]{ -Equivalent to @racket[struct] and @racket[define-struct-lenses] combined. -@lenses-unstable-examples[ - (struct/lens foo (a b c) #:transparent) - (lens-view foo-a-lens (foo 1 2 3)) - (lens-set foo-a-lens (foo 1 2 3) 100) + Equivalent to @racket[struct] and @racket[define-struct-lenses] combined. + @lenses-examples[ + (struct/lens foo (a b c) #:transparent) + (lens-view foo-a-lens (foo 1 2 3)) + (lens-set foo-a-lens (foo 1 2 3) 100) ]}