hashes scribblized, preliminary; SK may wish to add more examples

This commit is contained in:
Matthias Felleisen 2012-06-28 12:19:08 -04:00
parent eeb5f2778a
commit 004127bbc7

View File

@ -24,6 +24,16 @@
(define one (list 1)) (define one (list 1))
;; b 69, c 42, e 61
(define list-for-hash '((b . 69) (c . 42) (e . 61) (r . 999)))
(define hsh (make-hash list-for-hash))
(define heq (make-hasheq list-for-hash))
(define heqv (make-hasheqv list-for-hash))
(define ish (make-immutable-hash list-for-hash))
(define ieq (make-immutable-hasheq list-for-hash))
(define ieqv (make-immutable-hasheqv list-for-hash))
(define q (make-posn "bye" 2)) (define q (make-posn "bye" 2))
(define p (make-posn 2 -3)) (define p (make-posn 2 -3))
@ -225,88 +235,162 @@
@defproc[((advanced-make-hash make-hash)) (hash X Y)]{ @defproc[((advanced-make-hash make-hash)) (hash X Y)]{
Constructs a mutable hash table from an optional list of mappings that Constructs a mutable hash table from an optional list of mappings that
uses equal? for comparisions. uses equal? for comparisions.
@interaction[#:eval (asl)
(make-hash)
(make-hash '((b 69) (e 61) (i 999)))
]
} }
@defproc[((advanced-make-hasheq make-hasheq)) (hash X Y)]{ @defproc[((advanced-make-hasheq make-hasheq)) (hash X Y)]{
Constructs a mutable hash table from an optional list of mappings that Constructs a mutable hash table from an optional list of mappings that
uses eq? for comparisions. uses eq? for comparisions.
@interaction[#:eval (asl)
(make-hasheq)
(make-hasheq '((b 69) (e 61) (i 999)))
]
} }
@defproc[((advanced-make-hasheqv make-hasheqv)) (hash X Y)]{ @defproc[((advanced-make-hasheqv make-hasheqv)) (hash X Y)]{
Constructs a mutable hash table from an optional list of mappings that Constructs a mutable hash table from an optional list of mappings that
uses eqv? for comparisions. uses eqv? for comparisions.
@interaction[#:eval (asl)
(make-hasheqv)
(make-hasheqv '((b 69) (e 61) (i 999)))
]
} }
@defproc[((advanced-make-immutable-hash make-immutable-hash)) (hash X Y)]{ @defproc[((advanced-make-immutable-hash make-immutable-hash)) (hash X Y)]{
Constructs an immutable hash table from an optional list of mappings Constructs an immutable hash table from an optional list of mappings
that uses equal? for comparisions. that uses equal? for comparisions.
@interaction[#:eval (asl)
(make-immutable-hash)
(make-immutable-hash '((b 69) (e 61) (i 999)))
]
} }
@defproc[((advanced-make-immutable-hasheq make-immutable-hasheq)) (hash X Y)]{ @defproc[((advanced-make-immutable-hasheq make-immutable-hasheq)) (hash X Y)]{
Constructs an immutable hash table from an optional list of mappings Constructs an immutable hash table from an optional list of mappings
that uses eq? for comparisions. that uses eq? for comparisions.
@interaction[#:eval (asl)
(make-immutable-hasheq)
(make-immutable-hasheq '((b 69) (e 61) (i 999)))
]
} }
@defproc[((advanced-make-immutable-hasheqv make-immutable-hasheqv)) (hash X Y)]{ @defproc[((advanced-make-immutable-hasheqv make-immutable-hasheqv)) (hash X Y)]{
Constructs an immutable hash table from an optional list of mappings Constructs an immutable hash table from an optional list of mappings
that uses eqv? for comparisions. that uses eqv? for comparisions.
@interaction[#:eval (asl)
(make-immutable-hasheqv)
(make-immutable-hasheqv '((b 69) (e 61) (i 999)))
]
} }
@defproc[(hash-set! [h (hash X Y)] [k X] [v Y]) void?]{ @defproc[(hash-set! [h (hash X Y)] [k X] [v Y]) void?]{
Updates a mutable hash table with a new mapping. Updates a mutable hash table with a new mapping.
@interaction[#:eval (asl) hsh (hash-set! hsh 'a 23) hsh]
} }
@defproc[(hash-set [h (hash X Y)] [k X] [v Y]) (hash X Y)]{ @defproc[(hash-set [h (hash X Y)] [k X] [v Y]) (hash X Y)]{
Constructs an immutable hash table with one new mapping from an Constructs an immutable hash table with one new mapping from an
existing immutable hash table. existing immutable hash table.
@interaction[#:eval (asl) (hash-set ish 'a 23)]
} }
@defproc[(hash-ref [h (hash X Y)] [k X]) Y]{ @defproc[(hash-ref [h (hash X Y)] [k X]) Y]{
Extracts the value associated with a key from a hash table; the three Extracts the value associated with a key from a hash table; the three
; argument case allows a default value or default value computation. ; argument case allows a default value or default value computation.
@interaction[#:eval (asl) hsh (hash-ref hsh 'b)]
} }
@defproc[(hash-ref! [h (hash X Y)] [k X] [v Y]) Y]{ @defproc[(hash-ref! [h (hash X Y)] [k X] [v Y]) Y]{
Extracts the value associated with a key from a mutable hash table; if Extracts the value associated with a key from a mutable hash table; if
; the key does not have an mapping, the third argument is used as the ; the key does not have an mapping, the third argument is used as the
; value (or used to compute the value) and is added to the hash table ; value (or used to compute the value) and is added to the hash table
; associated with the key. ; associated with the key.
@interaction[#:eval (asl) hsh (hash-ref! hsh 'd 99) hsh]
} }
@defproc[(hash-update! [h (hash X Y)] [k X] [f (Y -> Y)]) void?]{ @defproc[(hash-update! [h (hash X Y)] [k X] [f (Y -> Y)]) void?]{
Composes hash-ref and hash-set! to update an existing mapping; the Composes hash-ref and hash-set! to update an existing mapping; the
; third argument is used to compute the new mapping value; the fourth ; third argument is used to compute the new mapping value; the fourth
; argument is used as the third argument to hash-ref. ; argument is used as the third argument to hash-ref.
@interaction[#:eval (asl) hsh (hash-update! hsh 'b (lambda (old-b) (+ old-b 1))) hsh]
} }
@defproc[(hash-update [h (hash X Y)] [k X] [f (Y -> Y)]) (hash X Y)]{ @defproc[(hash-update [h (hash X Y)] [k X] [f (Y -> Y)]) (hash X Y)]{
Composes hash-ref and hash-set to update an existing mapping; the third Composes hash-ref and hash-set to update an existing mapping; the third
; argument is used to compute the new mapping value; the fourth ; argument is used to compute the new mapping value; the fourth
; argument is used as the third argument to hash-ref. ; argument is used as the third argument to hash-ref.
@interaction[#:eval (asl) (hash-update ish 'b (lambda (old-b) (+ old-b 1)))]
} }
@defproc[(hash-has-key? [h (hash X Y)] [x X]) boolean]{ @defproc[(hash-has-key? [h (hash X Y)] [x X]) boolean]{
Determines if a key is associated with a value in a hash table. Determines if a key is associated with a value in a hash table.
@interaction[#:eval (asl)
ish
(hash-has-key? ish 'a)
hsh
(hash-has-key? hsh 'd)]
} }
@defproc[(hash-remove! [h (hash X Y)] [x X]) void]{ @defproc[(hash-remove! [h (hash X Y)] [x X]) void]{
Removes an mapping from a mutable hash table. Removes an mapping from a mutable hash table.
@interaction[#:eval (asl)
hsh
(hash-remove! hsh 'r)
hsh]
} }
@defproc[(hash-remove [h (hash X Y)] [k X]) (hash X Y)]{ @defproc[(hash-remove [h (hash X Y)] [k X]) (hash X Y)]{
Constructs an immutable hash table with one less mapping than an Constructs an immutable hash table with one less mapping than an
existing immutable hash table. existing immutable hash table.
@interaction[#:eval (asl)
ish
(hash-remove ish 'b)]
} }
@defproc[(hash-map [h (hash X Y)] [f (X Y -> Z)]) (listof Z)]{ @defproc[(hash-map [h (hash X Y)] [f (X Y -> Z)]) (listof Z)]{
Constructs a new list by applying a function to each mapping of a hash Constructs a new list by applying a function to each mapping of a hash
table. table.
@interaction[#:eval (asl)
ish
(hash-map ish list)]
} }
@defproc[(hash-for-each [h (hash X Y)] [f (X Y -> any)]) void?]{ @defproc[(hash-for-each [h (hash X Y)] [f (X Y -> any)]) void?]{
Applies a function to each mapping of a hash table for effect only. Applies a function to each mapping of a hash table for effect only.
@interaction[#:eval (asl)
hsh
(hash-for-each hsh (lambda (ky vl) (hash-set! hsh ky (+ vl 1))))
hsh]
} }
@defproc[(hash-count [h hash]) integer]{ @defproc[(hash-count [h hash]) integer]{
Determines the number of keys mapped by a hash table. Determines the number of keys mapped by a hash table.
@interaction[#:eval (asl)
ish
(hash-count ish)]
} }
@defproc[(hash-copy [h hash]) hash]{ @defproc[(hash-copy [h hash]) hash]{
Copies a hash table. Copies a hash table.
} }
@defproc[(hash? [x any]) boolean]{ @defproc[(hash? [x any]) boolean]{
Determines if a value is a hash table. Determines if a value is a hash table.
@interaction[#:eval (asl)
ish
(hash? ish)
(hash? 42)]
} }
@defproc[(hash-equal? [h hash?]) boolean]{ @defproc[(hash-equal? [h hash?]) boolean]{
Determines if a hash table uses equal? for comparisons. Determines if a hash table uses equal? for comparisons.
@interaction[#:eval (asl)
ish
(hash-equal? ish)
ieq
(hash-equal? ieq)
]
} }
@defproc[(hash-eq? [h hash]) boolean]{ @defproc[(hash-eq? [h hash]) boolean]{
Determines if a hash table uses eq? for comparisons. Determines if a hash table uses eq? for comparisons.
@interaction[#:eval (asl)
hsh
(hash-eq? hsh)
heq
(hash-eq? heq)
]
} }
@defproc[(hash-eqv? [h hash]) boolean]{ @defproc[(hash-eqv? [h hash]) boolean]{
Determines if a hash table uses eqv? for comparisons. Determines if a hash table uses eqv? for comparisons.
@interaction[#:eval (asl)
heq
(hash-eqv? heq)
heqv
(hash-eqv? heqv)
]
})) }))
#| #|