application/octet-stream
•
1.88 KB
•
61 lines
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Some functions about trees
(defn tree/zip (keys values)
"Return a tree where KEYS point to VALUES"
(def ret {})
(doseq (key keys ret)
(set! ret key (car values))
(cdr! values)))
(defn tree/+= (t k v)
"Increment value at K in T by V"
(set! t k (+ v (int (or (ref t k) 0)))))
(defmacro tree/-= (t k v)
"Decrement value at K in T by V"
`(tree/+= ~t ~k (- ~v)))
(defmacro tree/++ (t k)
"Increment value at K in T by 1"
`(tree/+= ~t ~k 1))
(defmacro tree/-- (t k)
"Increment value at K in T by 1"
`(tree/-= ~t ~k 1))
(defn tree/equal? (a b)
"Compares two trees for equality"
(if (and (tree? a)
(tree? b))
(and (= (tree/key* a)
(tree/key* b))
(equal? (tree/value* a)
(tree/value* b))
(tree/equal? (tree/left* a)
(tree/left* b))
(tree/equal? (tree/right* a)
(tree/right* b)))
(equal? a b)))
(defn tree/reduce (l o s)
"Combine all elements in l using operation o and starting value s"
(list/reduce (tree/values l) o s))
(defn tree/filter (l f)
"Return a new tree with all elements from L where F returns true"
(def ret {})
(doseq (e (tree/keys l) ret)
(def t (ref l e))
(when (f t)
(set! ret e t))))
(defn tree/merge (a b)
"Merge two trees together, if a key is contained in both trees the on in B gets priority"
(when-not b (return (if a (:clone a) {})))
(when-not a (return (:clone b)))
(def ret (:clone a))
(doseq (k (tree/keys b) ret)
(set! ret k (ref b k))))