diff options
| author | 2024-09-26 21:46:01 -0400 | |
|---|---|---|
| committer | 2024-09-26 21:46:01 -0400 | |
| commit | 3c34c4a5a7253df4417420bf276a78f8e9e1969b (patch) | |
| tree | a25a6cf59a1e5543e46195938f1ed65110b81aba /set.scm | |
| parent | add object helper functions (diff) | |
object: change to a stateful table
Diffstat (limited to 'set.scm')
| -rw-r--r-- | set.scm | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -467,6 +467,42 @@ smap pairs))) +;;; SYMBOL-TABLE: +;;; +;;; A stateful map from symbols to values. +;;; +;;; (SET! KEY VAL) +;;; (DELETE! KEY) +;;; (GET KEY) +;;; (TYPE) +(define symbol-table + (lambda () + (let ((table '())) + (letrec ((insert! + (lambda (key val) + (let ((ret (smap:insert table + (symbol->string key) + val))) + (set! table (car ret)) + (cdr ret)))) + (delete! (lambda (key) (smap:delete table (symbol->string + key)))) + (search + (lambda (key . default) + (let ((ret (smap:search table (symbol->string key)))) + (if (null? ret) + (if (null? default) + #f + (car default)) + (map:val ret)))))) + (lambda (op . args) + (cond + ((eq? op 'set!) (apply insert! args)) + ((eq? op 'delete!) (apply delete! args)) + ((eq? op 'get) (apply search args)) + ((eq? op 'type) 'symbol-table) + (else (error 'symbol-table 'unknown op args)))))))) + ;;; ;;;;; ;;; Tests ;;; ;;;;; |
