Skip to content

Instantly share code, notes, and snippets.

@Bronsa
Created November 19, 2012 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bronsa/4113896 to your computer and use it in GitHub Desktop.
Save Bronsa/4113896 to your computer and use it in GitHub Desktop.
(def tree '[:and a [:or b [:or c d]]])
(def db '{:a #{a d} :b #{b c} :d #{a c}})
;; (defn compile* [o el]
;; (if (coll? o)
;; (compile o el)
;; (get el o)))
;; (defmulti compile (fn [[op r l] el] op))
;; (defmethod compile :and [[ _ r l] el]
;; (and (get el r) (compile* l el)))
;; (defmethod compile :or [[ _ r l] el]
;; (or (get el r) (compile* l el)))
;; (defmethod compile :not [[ _ r _] el]
;; (not (get el r)))
;; (defn exp-filter [tree db]
;; (filter (fn [[k v]] (compile tree v)) db))
(def tags '{a #{:a :d}
b #{:b}
c #{:b :d}
d #{:a}})
(require '[clojure.set :as s])
(defmulti compile (fn [[op _ _] _ _] op))
(defn compile* [o el tags]
(if (coll? o)
(compile o el tags)
(get tags o)))
(defmethod compile :and [[ _ r l] keys tags]
(s/intersection (get tags r)
(compile* l keys tags)))
(defmethod compile :or [[ _ r l] keys tags]
(s/union (get tags r)
(compile* l keys tags)))
(defmethod compile :not [[ _ r _] keys tags]
(s/difference (set keys) (get tags r)))
(defn exp-filter [tree db tags]
(compile tree (keys db) tags))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment