diff options
author | Ben Sima <ben@bsima.me> | 2020-05-13 13:32:14 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2020-05-13 13:32:14 -0700 |
commit | e6ad8129cc854dac38940acb24e3a07cd0cd7b56 (patch) | |
tree | dfadd90e9039184e3d1415cdcddfcd39108e9d5c /bs/dict.scm | |
parent | 5fc1cc6602ac03d05ce004d8d4d9c712242f318b (diff) |
Re-namespace general scheme code to bs
Diffstat (limited to 'bs/dict.scm')
-rw-r--r-- | bs/dict.scm | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/bs/dict.scm b/bs/dict.scm new file mode 100644 index 0000000..ac55ba7 --- /dev/null +++ b/bs/dict.scm @@ -0,0 +1,50 @@ +#!r6rs +;; 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))) |