Adding hash->list
This commit is contained in:
parent
db87add6b7
commit
58ad011ce9
|
@ -1,10 +1,13 @@
|
|||
(module hash "pre-base.rkt"
|
||||
(define (hash-domain table)
|
||||
(for/list ([i (in-hash-keys table)]) i))
|
||||
(hash-map table (λ (k v) k)))
|
||||
|
||||
(define (hash-range table)
|
||||
(for/list ([i (in-hash-values table)]) i))
|
||||
(hash-map table (λ (k v) v)))
|
||||
|
||||
(define (hash->list table)
|
||||
(hash-map table cons))
|
||||
|
||||
(provide hash-domain
|
||||
hash-range)
|
||||
)
|
||||
hash-range
|
||||
hash->list))
|
|
@ -314,6 +314,12 @@ Returns a list of the keys of @scheme[hash] in an unspecified order.
|
|||
(listof any/c)]{
|
||||
Returns a list of the values of @scheme[hash] in an unspecified order.
|
||||
|
||||
@see-also-concurrency-caveat[]}
|
||||
|
||||
@defproc[(hash->list [hash hash?])
|
||||
(listof (cons/c any/c any/c))]{
|
||||
Returns a list of the key--value pairs of @scheme[hash] in an unspecified order.
|
||||
|
||||
@see-also-concurrency-caveat[]}
|
||||
|
||||
@defproc[(hash-for-each [hash hash?]
|
||||
|
|
|
@ -2365,6 +2365,7 @@
|
|||
|
||||
(test (list 1 2 3) hash-domain #hasheq((1 . 'a)(2 . 'b)(3 . 'c)))
|
||||
(test (list 'a 'b 'c) hash-range #hasheq((1 . 'a)(2 . 'b)(3 . 'c)))
|
||||
(test (list (cons 1 'a) (cons 2 'b) (cons 3 'c)) hash->list #hasheq((1 . 'a)(2 . 'b)(3 . 'c)))
|
||||
|
||||
(arity-test make-immutable-hash 1 1)
|
||||
(arity-test make-immutable-hasheq 1 1)
|
||||
|
|
|
@ -4,10 +4,6 @@
|
|||
|
||||
(run-tests
|
||||
(test-suite "hash.ss"
|
||||
(test-suite "hash-equal?"
|
||||
(test (check-true (hash-equal? #hash())))
|
||||
(test (check-false (hash-equal? #hasheq())))
|
||||
(test (check-false (hash-equal? #hasheqv()))))
|
||||
(test-suite "hash-ref/check"
|
||||
(test-ok (check-equal? (hash-ref/check #hash([1 . one] [2 . two]) 1)
|
||||
'one))
|
||||
|
@ -31,14 +27,6 @@
|
|||
(check-equal? (hash-ref/failure #hash([1 . one] [2 . two]) 3 f)
|
||||
8)
|
||||
(check-equal? x 8)))
|
||||
(test-suite "hash-has-key?"
|
||||
(test-ok (check-equal? (hash-has-key? #hash([1 . one] [2 . two]) 1) #t))
|
||||
(test-ok (check-equal? (hash-has-key? #hash([1 . one] [2 . two]) 3) #f)))
|
||||
(test-suite "hash-domain"
|
||||
(test-ok (check-equal? (hash-domain #hash([1 . one] [2 . two])) '(1 2))))
|
||||
(test-suite "hash-range"
|
||||
(test-ok (check-equal? (hash-range #hash([1 . one] [2 . two]))
|
||||
'(one two))))
|
||||
(test-suite "hash-union"
|
||||
(test-ok (hash-union #hash([1 . one] [2 . two])
|
||||
#hash([3 . three] [4 . four]))
|
||||
|
|
|
@ -67,57 +67,6 @@ applying @scheme[f] (in tail position) if @scheme[h] has no entry for
|
|||
|
||||
}
|
||||
|
||||
@section{Hash Table Accessors}
|
||||
|
||||
@defproc[(hash-equal? [h hash?]) boolean?]{
|
||||
|
||||
Reports whether @scheme[h] maps keys according to @scheme[equal?].
|
||||
|
||||
@defexamples[
|
||||
#:eval (eval/require 'unstable/hash)
|
||||
(hash-equal? #hash())
|
||||
(hash-equal? #hasheq())
|
||||
(hash-equal? #hasheqv())
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
@defproc[(hash-has-key? [h hash?] [k any/c]) boolean?]{
|
||||
|
||||
Reports whether @scheme[h] has an entry for @scheme[k]. This function is
|
||||
re-exported from @schememodname[scheme/base]. In versions of Racket before
|
||||
@scheme[hash-has-key?] was implemented, this module provides its own definition.
|
||||
|
||||
@defexamples[
|
||||
#:eval (eval/require 'unstable/hash)
|
||||
(hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2)
|
||||
(hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4)
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
@defproc[(hash-domain [h hash?]) list?]{
|
||||
|
||||
Produces the domain of a hash table as a list of keys.
|
||||
|
||||
@defexamples[
|
||||
#:eval (eval/require 'unstable/hash)
|
||||
(hash-domain (make-immutable-hash '([1 . one] [2 . two] [3 . three])))
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
@defproc[(hash-range [h hash?]) list?]{
|
||||
|
||||
Produces the range of a hash table as a list of values.
|
||||
|
||||
@defexamples[
|
||||
#:eval (eval/require 'unstable/hash)
|
||||
(hash-range (make-immutable-hash '([1 . one] [2 . two] [3 . three])))
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
@section{Hash Table Combinations}
|
||||
|
||||
@defproc[(hash-union [h0 (and/c hash? hash-can-functional-set?)]
|
||||
|
|
Loading…
Reference in New Issue
Block a user