summaryrefslogtreecommitdiff
path: root/bs/dict.scm
blob: a91d7110f40b2f4eae133c68963910986c0da5bb (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
;; Experimental python-like dictionary.  Import this qualified, eg:
;;     (import (prefix (dict) dict/))
;; or
;;     (import (prefix (dict) dict.))
(library (bs dict (2))
  (export
   empty new dict dict? keys vals contains?
   set get del update map)
  (import
    (rnrs base)
    (srfi srfi-69))

  (define (empty)
    (make-hash-table))

  (define (new tuples)
    (let ([dict (make-hash-table)])
      (map (lambda (k . v) (hash-table-set! dict k v)) tuples)
      dict))

  (define (dict? dict)
    (hash-table? dict))

  (define (keys dict)
    (hash-table-keys dict))

  (define (vals dict)
    (hash-table-values dict))

  (define (len dict)
    (hash-table-size dict))

  (define (contains? dict key)
    (hash-table-exists? dict key))

  (define (set dict key value)
    (hash-table-set! dict key value))

  (define (get dict key)
    (hash-table-ref dict key #f))

  (define (del dict key)
    (hash-table-delete! dict key))

  (define (update dict key func)
    (hash-table-update! dict key func (lambda () '())))

  (define (map func dict)
    (hash-table-walk dict func)))