Fix hash and struct docs

This commit is contained in:
Jack Firth 2015-08-18 16:11:21 -07:00
parent 11fc744bfe
commit 31a8d064fd
10 changed files with 84 additions and 60 deletions

View File

@ -1,10 +1,12 @@
#lang racket/base #lang racket/base
(require "hash.rkt" (require "nested.rkt"
"pick.rkt") "pick.rkt"
"ref.rkt")
(provide (provide
(all-from-out "hash.rkt" (all-from-out "nested.rkt"
"pick.rkt")) "pick.rkt"
"ref.rkt"))

7
lens/hash/main.scrbl Normal file
View File

@ -0,0 +1,7 @@
#lang scribble/manual
@title{Hash Lenses}
@include-section["ref.scrbl"]
@include-section["nested.scrbl"]
@include-section["pick.scrbl"]

View File

@ -1,36 +1,25 @@
#lang racket/base #lang racket
(require racket/contract (require "../base/main.rkt"
fancy-app
"../base/main.rkt"
"../compound/main.rkt" "../compound/main.rkt"
"../util/immutable.rkt" "../util/immutable.rkt"
"../util/rest-contract.rkt") "../util/rest-contract.rkt"
"ref.rkt")
(module+ test (module+ test
(require rackunit)) (require rackunit
fancy-app))
(provide (provide
(contract-out (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))])) [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) (define (hash-ref-nested-lens . keys)
(apply lens-thrush (map hash-ref-lens keys))) (apply lens-thrush (map hash-ref-lens keys)))
(module+ test (module+ test
(define a (hash-ref-lens 'a))
(define a-x (hash-ref-nested-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)))
(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)) (let-lens [val ctxt] a-x (hash 'a (hash 'x 1 'y 2) 'b (hash 'z 3))
(check-equal? val 1) (check-equal? val 1)
(check-equal? (ctxt 100) (hash 'a (hash 'x 100 'y 2) 'b (hash 'z 3)))) (check-equal? (ctxt 100) (hash 'a (hash 'x 100 'y 2) 'b (hash 'z 3))))

View File

@ -1,19 +1,8 @@
#lang scribble/manual #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?]{ @defproc[(hash-ref-nested-lens [key any/c] ...) lens?]{
Contructs a lens that targets hashes with nested hashes Contructs a lens that targets hashes with nested hashes
as values and views the value obtained by using each as values and views the value obtained by using each

View File

@ -6,7 +6,7 @@
"../compound/join-hash.rkt" "../compound/join-hash.rkt"
"../util/immutable.rkt" "../util/immutable.rkt"
"../util/rest-contract.rkt" "../util/rest-contract.rkt"
"hash.rkt") "ref.rkt")
(module+ test (module+ test
(require rackunit)) (require rackunit))

View File

@ -1,14 +1,13 @@
#lang scribble/manual #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?]{ @defproc[(hash-pick-lens [key any/c] ...) lens?]{
Creates a lens that views a subset of the target hash-table with the given 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 @racket[key]s. The view, is another hash-table with only the given keys and
their corrosponding values in the target hash-table. their corrosponding values in the target hash-table.
@lenses-examples[ @lenses-examples[
(lens-view (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3)) (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)) (lens-set (hash-pick-lens 'a 'c) (hash 'a 1 'b 2 'c 3) (hash 'a 4 'c 5))
]} ]}

26
lens/hash/ref.rkt Normal file
View File

@ -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)))

13
lens/hash/ref.scrbl Normal file
View File

@ -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)
]}

View File

@ -3,12 +3,12 @@
(require (require
"base/main.rkt" "base/main.rkt"
"compound/main.rkt" "compound/main.rkt"
"dict.rkt"
"hash/main.rkt" "hash/main.rkt"
"list/main.rkt" "list/main.rkt"
"struct/main.rkt"
"dict.rkt"
"stream.rkt" "stream.rkt"
"string.rkt" "string.rkt"
"struct/main.rkt"
"vector/main.rkt") "vector/main.rkt")
(provide (provide
@ -16,13 +16,13 @@
(all-from-out (all-from-out
"base/main.rkt" "base/main.rkt"
"compound/main.rkt" "compound/main.rkt"
"dict.rkt"
"hash/main.rkt" "hash/main.rkt"
"list/main.rkt" "list/main.rkt"
"struct/main.rkt"
"vector/main.rkt"
"dict.rkt"
"stream.rkt" "stream.rkt"
"string.rkt") "string.rkt"
"struct/main.rkt"
"vector/main.rkt")
focus-lens focus-lens
drop-lens drop-lens
take-lens take-lens

View File

@ -2,11 +2,10 @@
@(require "../doc-util/main.rkt") @(require "../doc-util/main.rkt")
@defmodule[unstable/lens/struct]
@defform[(define-struct-lenses struct-id)]{ @defform[(define-struct-lenses struct-id)]{
Given a @racket[struct-id], defines a lens for each of its fields. Given a @racket[struct-id], defines a lens for each of its fields.
@lenses-unstable-examples[ @lenses-examples[
(struct foo (a b c) #:transparent) (struct foo (a b c) #:transparent)
(define-struct-lenses foo) (define-struct-lenses foo)
(lens-view foo-a-lens (foo 1 2 3)) (lens-view foo-a-lens (foo 1 2 3))
@ -14,8 +13,8 @@ Given a @racket[struct-id], defines a lens for each of its fields.
]} ]}
@defform[(struct/lens struct-id (field-spec ...) struct-option ...)]{ @defform[(struct/lens struct-id (field-spec ...) struct-option ...)]{
Equivalent to @racket[struct] and @racket[define-struct-lenses] combined. Equivalent to @racket[struct] and @racket[define-struct-lenses] combined.
@lenses-unstable-examples[ @lenses-examples[
(struct/lens foo (a b c) #:transparent) (struct/lens foo (a b c) #:transparent)
(lens-view foo-a-lens (foo 1 2 3)) (lens-view foo-a-lens (foo 1 2 3))
(lens-set foo-a-lens (foo 1 2 3) 100) (lens-set foo-a-lens (foo 1 2 3) 100)