aboutsummaryrefslogtreecommitdiffstats
path: root/set.scm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2024-09-26 21:46:01 -0400
committerGravatar Peter McGoron 2024-09-26 21:46:01 -0400
commit3c34c4a5a7253df4417420bf276a78f8e9e1969b (patch)
treea25a6cf59a1e5543e46195938f1ed65110b81aba /set.scm
parentadd object helper functions (diff)
object: change to a stateful table
Diffstat (limited to 'set.scm')
-rw-r--r--set.scm36
1 files changed, 36 insertions, 0 deletions
diff --git a/set.scm b/set.scm
index 52d10b6..6aa661d 100644
--- a/set.scm
+++ b/set.scm
@@ -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
;;; ;;;;;