Optional arguments to hash table constructors in ASL

This commit is contained in:
Jay McCarthy 2010-08-25 10:18:39 -06:00
parent 1f830cc2c6
commit 368d711ae2
3 changed files with 37 additions and 22 deletions

View File

@ -5,7 +5,7 @@
mzlib/pretty mzlib/pretty
syntax/docprovide syntax/docprovide
scheme/promise scheme/promise
scheme/port scheme/port
"../posn.ss" "../posn.ss"
(for-syntax scheme/base)) (for-syntax scheme/base))
@ -41,7 +41,7 @@
(write (any -> void) (write (any -> void)
"to print the argument to stdout (in a traditional style that is somewhere between print and display)") "to print the argument to stdout (in a traditional style that is somewhere between print and display)")
((pp pretty-print) (any -> void) ((pp pretty-print) (any -> void)
"like write, but with standard newlines and indentation") "like write, but with standard newlines and indentation")
(printf (string any ... -> void) (printf (string any ... -> void)
"to format the rest of the arguments according to the first argument and print it to stdout") "to format the rest of the arguments according to the first argument and print it to stdout")
(newline (-> void) (newline (-> void)
@ -67,11 +67,11 @@
("Misc" ("Misc"
(gensym (-> symbol?) (gensym (-> symbol?)
"to generate a new symbol, different from all symbols in the program") "to generate a new symbol, different from all symbols in the program")
(sleep (-> positive-number void) (sleep (-> positive-number void)
"to cause the program to sleep for the given number of seconds") "to cause the program to sleep for the given number of seconds")
(current-milliseconds (-> exact-integer) (current-milliseconds (-> exact-integer)
"to return the current “time” in fixnum milliseconds (possibly negative)") "to return the current “time” in fixnum milliseconds (possibly negative)")
(force (delay -> any) "to find the delayed value; see also delay") (force (delay -> any) "to find the delayed value; see also delay")
(promise? (any -> boolean) "to determine if a value is delayed") (promise? (any -> boolean) "to determine if a value is delayed")
@ -109,12 +109,21 @@
"to determine if a value is a box")) "to determine if a value is a box"))
("Hash Tables" ("Hash Tables"
((advanced-make-hash make-hash) ((listof (list X Y)) -> (hash X Y)) ((advanced-make-hash make-hash)
"to construct a hash table from a list of mappings that uses equal? for comparisions") (case->
((advanced-make-hasheq make-hasheq) ((listof (list X Y)) -> (hash X Y)) (-> (hash X Y))
"to construct a hash table from a list of mappings that uses eq? for comparisions") ((listof (list X Y)) -> (hash X Y)))
((advanced-make-hasheqv make-hasheqv) ((listof (list X Y)) -> (hash X Y)) "to construct a hash table from an optional list of mappings that uses equal? for comparisions")
"to construct a hash table from a list of mappings that uses eqv? for comparisions") ((advanced-make-hasheq make-hasheq)
(case->
(-> (hash X Y))
((listof (list X Y)) -> (hash X Y)))
"to construct a hash table from an optional list of mappings that uses eq? for comparisions")
((advanced-make-hasheqv make-hasheqv)
(case->
(-> (hash X Y))
((listof (list X Y)) -> (hash X Y)))
"to construct a hash table from an optional list of mappings that uses eqv? for comparisions")
(hash-set! ((hash X Y) X Y -> void) (hash-set! ((hash X Y) X Y -> void)
"to update a hash table with a new mapping") "to update a hash table with a new mapping")
(hash-ref (case-> (hash-ref (case->
@ -132,7 +141,7 @@
((hash X Y) X (Y -> Y) (-> Y) -> void)) ((hash X Y) X (Y -> Y) (-> Y) -> void))
"to compose 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 as the third argument to hash-ref") "to compose 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 as the third argument to hash-ref")
(hash-has-key? ((hash X Y) X -> boolean) (hash-has-key? ((hash X Y) X -> boolean)
"to determine if a key is associated with a value in a hash table") "to determine if a key is associated with a value in a hash table")
(hash-remove! ((hash X Y) X -> void) (hash-remove! ((hash X Y) X -> void)
"to remove an mapping from a hash table") "to remove an mapping from a hash table")
(hash-map ((hash X Y) (X Y -> A) -> (listof A)) (hash-map ((hash X Y) (X Y -> A) -> (listof A))
@ -140,14 +149,14 @@
(hash-for-each ((hash X Y) (X Y -> any) -> void) (hash-for-each ((hash X Y) (X Y -> any) -> void)
"to apply a function to each mapping of a hash table for effect only") "to apply a function to each mapping of a hash table for effect only")
(hash-count (hash -> integer) (hash-count (hash -> integer)
"to determine the number of keys mapped by a hash table") "to determine the number of keys mapped by a hash table")
(hash-copy (hash -> hash) (hash-copy (hash -> hash)
"to copy a hash table") "to copy a hash table")
(hash? (any -> boolean) (hash? (any -> boolean)
"to determine if a value is a hash table") "to determine if a value is a hash table")
(hash-equal? (hash -> boolean) (hash-equal? (hash -> boolean)
"to determine if a hash table uses equal? for comparisions") "to determine if a hash table uses equal? for comparisions")
(hash-eq? (hash -> boolean) (hash-eq? (hash -> boolean)
"to determine if a hash table uses eq? for comparisions") "to determine if a hash table uses eq? for comparisions")
(hash-eqv? (hash -> boolean) (hash-eqv? (hash -> boolean)
"to determine if a hash table uses eqv? for comparisions")))) "to determine if a hash table uses eqv? for comparisions"))))

View File

@ -346,15 +346,15 @@ namespace.
(apply append x))) (apply append x)))
(define-teach advanced make-hash (define-teach advanced make-hash
(lambda (a) (lambda ([a empty])
(make-hash (map (lambda (l) (cons (first l) (second l))) a)))) (make-hash (map (lambda (l) (cons (first l) (second l))) a))))
(define-teach advanced make-hasheq (define-teach advanced make-hasheq
(lambda (a) (lambda ([a empty])
(make-hasheq (map (lambda (l) (cons (first l) (second l))) a)))) (make-hasheq (map (lambda (l) (cons (first l) (second l))) a))))
(define-teach advanced make-hasheqv (define-teach advanced make-hasheqv
(lambda (a) (lambda ([a empty])
(make-hasheqv (map (lambda (l) (cons (first l) (second l))) a)))) (make-hasheqv (map (lambda (l) (cons (first l) (second l))) a))))
(provide (provide

View File

@ -258,6 +258,12 @@
(local [(define ht (make-hash (list (list 'a 1))))] (local [(define ht (make-hash (list (list 'a 1))))]
(begin (hash-update! ht 'b add1 (lambda () 1)) (begin (hash-update! ht 'b add1 (lambda () 1))
(hash-ref ht 'b)))) (hash-ref ht 'b))))
(htdp-test #t 'hash?
(hash? (make-hash)))
(htdp-test #t 'hash?
(hash? (make-hasheq)))
(htdp-test #t 'hash?
(hash? (make-hasheqv)))
(htdp-test #t 'hash? (htdp-test #t 'hash?
(hash? (make-hash (list (list 'a 1))))) (hash? (make-hash (list (list 'a 1)))))
(htdp-test #t 'hash? (htdp-test #t 'hash?