Created
November 19, 2012 21:06
-
-
Save Bronsa/4113896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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