Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
nujel / stdlib / collections / map.nuj
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Some functions using maps

(defn map/zip (keys values)
      "Return a Map where KEYS point to VALUES"
      (def ret (map/new))
      (doseq (key keys ret)
	     (set! ret key (car values))
	     (cdr! values)))

(defn map/reduce (l o s)
      "Combine all elements in l using operation o and starting value s"
      (list/reduce (:values l) o s))

(defn map/filter (l f)
      "Return a new Map with all elements from L where F returns true"
      (def ret (map/new))
      (doseq (e (:keys l) ret)
             (def t (ref l e))
             (when (f t)
                   (set! ret e t))))

(defn map/merge (a b)
      "Merge two Maps 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 (:keys b) ret)
             (set! ret k (ref b k))))