environments are now hashmaps
This commit is contained in:
+12
-10
@@ -3,11 +3,11 @@
|
||||
(alist::make alist::print alist::get alist::find alist::key-exists? alist::remove! alist::set! alist::set-overwrite!
|
||||
plist::make plist::print plist::get plist::find plist::prop-exists? plist::remove! plist::set! plist::set-overwrite!)
|
||||
|
||||
(define key-not-found-index -1)
|
||||
|
||||
(define-module alist
|
||||
:imports ("cxr.slime")
|
||||
:exports (make print get find key-exists? remove! set! set-overwrite!)
|
||||
(define key-not-found-index -1)
|
||||
|
||||
(define (make)
|
||||
(set-type!
|
||||
@@ -33,7 +33,7 @@
|
||||
(let ((associations (first alist)))
|
||||
(define (alist-get-intern associations key)
|
||||
(cond ((null? associations)
|
||||
(error "key was not found in alist"))
|
||||
(error :key-not-found "key was not found in alist"))
|
||||
((= (caar associations) key)
|
||||
(cdar associations))
|
||||
(else (alist-get-intern (rest associations) key))))
|
||||
@@ -52,12 +52,12 @@
|
||||
|
||||
|
||||
(define (key-exists? alist key)
|
||||
(not (= (alist-find alist key)
|
||||
(not (= (find alist key)
|
||||
key-not-found-index)))
|
||||
|
||||
|
||||
(define (remove! alist key)
|
||||
(let ((index (alist-find alist key)))
|
||||
(let ((index (find alist key)))
|
||||
(define (alist-remove!-internal asociations index)
|
||||
;; reminder: we only get called if we are not replacing the
|
||||
;; first element in the alist
|
||||
@@ -70,7 +70,7 @@
|
||||
;; else cdr-recurse
|
||||
(alist-remove!-internal (rest asociations) (- index 1))))
|
||||
|
||||
(cond ((= index key-not-found-index) (error "key to remove was not found"))
|
||||
(cond ((= index key-not-found-index) (error :key-not-found "key to remove was not found"))
|
||||
((= index 0) (mutate alist (pair (cdar alist) ())))
|
||||
(else (alist-remove!-internal alist index))))
|
||||
alist)
|
||||
@@ -87,7 +87,7 @@
|
||||
(define (alist-set-overwrite-intern associations key value)
|
||||
(cond ((= (caar associations) key)
|
||||
(mutate (car associations) (pair key value)))
|
||||
((null? associations) (alist-set! alist key value))
|
||||
((null? associations) (set! alist key value))
|
||||
(else (alist-set-overwrite-intern
|
||||
(rest associations) key value))))
|
||||
(alist-set-overwrite-intern associations key value))
|
||||
@@ -108,6 +108,8 @@
|
||||
;;
|
||||
;; '((:key1 value1 :key2 value2))
|
||||
|
||||
(define key-not-found-index -1)
|
||||
|
||||
(define (make)
|
||||
(set-type!
|
||||
'(())
|
||||
@@ -131,7 +133,7 @@
|
||||
(let ((props (first plist)))
|
||||
(define (plist-get-intern props prop)
|
||||
(cond ((null? props)
|
||||
(error "property was not found in plist"))
|
||||
(error :key-not-found "property was not found in plist"))
|
||||
((= (car props) prop)
|
||||
(cadr props))
|
||||
(else (plist-get-intern (cddr props) prop))))
|
||||
@@ -162,11 +164,11 @@
|
||||
(plist-find-intern props prop 0)))
|
||||
|
||||
(define (prop-exists? plist prop)
|
||||
(not (= (plist-find plist prop)
|
||||
(not (= (find plist prop)
|
||||
key-not-found-index)))
|
||||
|
||||
(define (remove! plist prop)
|
||||
(let ((index (plist-find plist prop)))
|
||||
(let ((index (find plist prop)))
|
||||
(define (plist-remove!-internal props index)
|
||||
;; reminder: we only get called if we are not replacing the
|
||||
;; first element in the alist
|
||||
@@ -179,7 +181,7 @@
|
||||
;; else cdr-recurse
|
||||
(plist-remove!-internal (cddr props) (- index 1))))
|
||||
|
||||
(cond ((= index key-not-found-index) (error "prop to remove was not found"))
|
||||
(cond ((= index key-not-found-index) (error :key-not-found "prop to remove was not found"))
|
||||
((= index 0) (mutate plist (pair (cddar plist) ())))
|
||||
(else (plist-remove!-internal plist index))))
|
||||
plist)
|
||||
|
||||
Reference in New Issue
Block a user