Fix hash and struct docs
This commit is contained in:
parent
11fc744bfe
commit
31a8d064fd
|
@ -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"))
|
||||
|
|
7
lens/hash/main.scrbl
Normal file
7
lens/hash/main.scrbl
Normal file
|
@ -0,0 +1,7 @@
|
|||
#lang scribble/manual
|
||||
|
||||
@title{Hash Lenses}
|
||||
|
||||
@include-section["ref.scrbl"]
|
||||
@include-section["nested.scrbl"]
|
||||
@include-section["pick.scrbl"]
|
|
@ -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))))
|
|
@ -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
|
|
@ -6,7 +6,7 @@
|
|||
"../compound/join-hash.rkt"
|
||||
"../util/immutable.rkt"
|
||||
"../util/rest-contract.rkt"
|
||||
"hash.rkt")
|
||||
"ref.rkt")
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
#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
|
||||
|
|
26
lens/hash/ref.rkt
Normal file
26
lens/hash/ref.rkt
Normal 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
13
lens/hash/ref.scrbl
Normal 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)
|
||||
]}
|
|
@ -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
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
|
||||
@(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[
|
||||
@lenses-examples[
|
||||
(struct foo (a b c) #:transparent)
|
||||
(define-struct-lenses foo)
|
||||
(lens-view foo-a-lens (foo 1 2 3))
|
||||
|
@ -15,7 +14,7 @@ Given a @racket[struct-id], defines a lens for each of its fields.
|
|||
|
||||
@defform[(struct/lens struct-id (field-spec ...) struct-option ...)]{
|
||||
Equivalent to @racket[struct] and @racket[define-struct-lenses] combined.
|
||||
@lenses-unstable-examples[
|
||||
@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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user