Login
7 branches 0 tags
Ben (X13/Arch) Removed nos/yield system, since they should be rewritten 0269074 3 years ago 752 Commits
nujel / stdlib / collections / tree.nuj
;; 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]
	     [tree/set! ret key [car values]]
	     [cdr! values]]
      ret]

[defn tree/+= [t k v]
      "Increment value at K in T by V"
      [tree/set! t k [+ v [int [or [tree/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 retunrs true"
      [def ret @[]]
      [doseq [e [tree/keys l]]
             [def t [tree/ref l e]]
             [when [f t]
                   [tree/set! ret e t]]]
      ret]