-
Notifications
You must be signed in to change notification settings - Fork 4
/
hash-table.ss
35 lines (28 loc) · 943 Bytes
/
hash-table.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#lang mzscheme
(require "base.ss"
"list.ss")
; pair ... -> hash-table
(define (make-hash-table/pairs . pairs)
(let ([table (make-hash-table)])
(alist-for-each
(lambda (key value)
(hash-table-put! table key value))
pairs)
table))
; hash-table any -> boolean
(define (hash-table-mapped? table key)
(let/ec escape
(hash-table-get table key (cut escape #f))
#t))
; hash-table -> (listof any)
(define (hash-table-keys table)
(hash-table-map table (lambda (k v) k)))
; hash-table -> (listof any)
(define (hash-table-values table)
(hash-table-map table (lambda (k v) v)))
; Provide statements -----------------------------
(provide/contract
[make-hash-table/pairs (->* () () #:rest (listof pair?) hash-table?)]
[hash-table-mapped? (-> hash-table? any/c boolean?)]
[hash-table-keys (-> hash-table? (or/c pair? null?))]
[hash-table-values (-> hash-table? (or/c pair? null?))])