alists and plists now
This commit is contained in:
+94
-3
@@ -13,10 +13,29 @@
|
||||
;; key1 key2
|
||||
;;
|
||||
;; '(((key1 . value1) (key2 . value2)))
|
||||
|
||||
;; plist:
|
||||
;; [ |/]
|
||||
;; |
|
||||
;; V
|
||||
;; [ | ]------------->[ | ]-------------> ...
|
||||
;; | |
|
||||
;; V V
|
||||
;; :key1 value1
|
||||
;;
|
||||
;; '((:key1 value1 :key2 value2))
|
||||
|
||||
(define key-not-found-index -1)
|
||||
|
||||
(define (make-alist)
|
||||
'(()))
|
||||
(set-type
|
||||
'(())
|
||||
:alist))
|
||||
|
||||
(define (make-plist)
|
||||
(set-type
|
||||
'(())
|
||||
:plist))
|
||||
|
||||
(define (pprint-alist alist)
|
||||
(let ((associations (first alist)))
|
||||
@@ -32,6 +51,19 @@
|
||||
(pprint-intern associations))
|
||||
(print ")\n")))
|
||||
|
||||
(define (pprint-plist plist)
|
||||
(let ((props (first plist)))
|
||||
(define (pprint-intern props)
|
||||
(when props
|
||||
(printf " "
|
||||
(car props) "->"
|
||||
(cadr props))
|
||||
(pprint-intern (cddr props))))
|
||||
(print "(")
|
||||
(when props
|
||||
(print "\n")
|
||||
(pprint-intern props))
|
||||
(print ")\n")))
|
||||
|
||||
(define (alist-get alist key)
|
||||
(let ((associations (first alist)))
|
||||
@@ -76,7 +108,8 @@
|
||||
|
||||
(cond ((= index key-not-found-index) (error "key to remove was not found"))
|
||||
((= index 0) (mutate alist (pair (cdar alist) ())))
|
||||
(else (alist-remove!-internal alist index)))))
|
||||
(else (alist-remove!-internal alist index))))
|
||||
alist)
|
||||
|
||||
|
||||
(define (alist-set! alist key value)
|
||||
@@ -93,4 +126,62 @@
|
||||
((null? associations) (alist-set! alist key value))
|
||||
(else (alist-set-overwrite-intern
|
||||
(rest associations) key value))))
|
||||
(alist-set-overwrite-intern associations key value)))
|
||||
(alist-set-overwrite-intern associations key value))
|
||||
alist)
|
||||
|
||||
|
||||
(define (plist-get plist prop)
|
||||
(let ((props (first plist)))
|
||||
(define (plist-get-intern props prop)
|
||||
(cond ((null? props)
|
||||
(error "property was not found in plist"))
|
||||
((= (car props) prop)
|
||||
(cadr props))
|
||||
(else (plist-get-intern (cddr props) prop))))
|
||||
(plist-get-intern props prop)))
|
||||
|
||||
(define (plist-set! plist prop value)
|
||||
(mutate plist (pair (pair prop (pair value (first plist))) ())))
|
||||
|
||||
(define (plist-set-overwrite! plist prop value)
|
||||
(let ((props (first plist)))
|
||||
(define (plist-set-overwrite-intern props prop value)
|
||||
(cond ((= (car props) prop)
|
||||
(mutate (cdr props) (pair value (cddr props))))
|
||||
((null? props) (plist-set! plist prop value))
|
||||
(else (plist-set-overwrite-intern
|
||||
(cddr props) prop value))))
|
||||
(plist-set-overwrite-intern props prop value))
|
||||
plist)
|
||||
|
||||
(define (plist-find plist prop)
|
||||
(let ((props (first plist)))
|
||||
(define (plist-find-intern props prop current-index)
|
||||
(cond ((null? props) key-not-found-index)
|
||||
((= (car props) prop) current-index)
|
||||
(else (plist-find-intern (cddr props) prop
|
||||
(+ 1 current-index)))))
|
||||
(plist-find-intern props prop 0)))
|
||||
|
||||
(define (plist-prop-exists? plist prop)
|
||||
(not (= (plist-find plist prop)
|
||||
key-not-found-index)))
|
||||
|
||||
(define (plist-remove! plist prop)
|
||||
(let ((index (plist-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
|
||||
;; reminder2: we know that the key exists
|
||||
(if (= index 1)
|
||||
;; we want to remove the next one, so we set our
|
||||
;; cdr to the next next one
|
||||
(mutate (cdar props) (pair (cadar props) ;; xD nice meme dude!!!
|
||||
(cdr (cdr (cdr (cdar props))))))
|
||||
;; else cdr-recurse
|
||||
(plist-remove!-internal (cddr props) (- index 1))))
|
||||
|
||||
(cond ((= index key-not-found-index) (error "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