Created
August 27, 2012 09:11
-
-
Save martintrojer/3486837 to your computer and use it in GitHub Desktop.
Datalog blog
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
(defn join-test3 [xs ys] | |
(let [db-base (make-database | |
(relation :last-first-email [:last :first :email]) | |
(index :last-first-email :email) | |
(relation :email-height [:email :height]) | |
(index :email-height :email)) | |
rules (rules-set | |
(<- (:first-height :first ?f :height ?h) | |
(:last-first-email :last ?l :first ?f :email ?e) | |
(:email-height :email ?e :height ?h))) | |
workplan (build-work-plan | |
rules | |
(?- :first-height :first ?f :height ?h)) | |
db (time (->> xs | |
(map (fn [[l f e]] | |
[:last-first-email :last l :first f :email e])) | |
(apply add-tuples db-base))) | |
db (time (->> ys | |
(map (fn [[e h]] [:email-height :email e :height h])) | |
(apply add-tuples db)))] | |
(run-work-plan workplan db {}))) |
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 db-base (make-database | |
(relation :last-first-email [:last :first :email]) | |
(relation :email-height [:email :height]))) | |
(def db | |
(add-tuples db-base | |
[:last-first-email :last "Doe" :first "John" :email "[email protected]"] | |
[:last-first-email :last "Doe" :first "Jane" :email "[email protected]"] | |
[:email-height :email "[email protected]" :height 73] | |
[:email-height :email "[email protected]" :height 71])) | |
(def rules (rules-set | |
(<- (:first-height :first ?f :height ?h) | |
(:last-first-email :last ?l :first ?f :email ?e) | |
(:email-height :email ?e :height ?h)))) | |
(def wp (build-work-plan | |
rules | |
(?- :first-height :first ?f :height ?h))) | |
(run-work-plan wp db {}) | |
;; ({:height 73, :first "Jane"} {:height 71, :first "John"}) |
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
(q '[:find ?first ?height | |
:in $a $b | |
:where [$a ?last ?first ?email] [$b ?email ?height]] | |
[["Doe" "John" "[email protected]"] | |
["Doe" "Jane" "[email protected]"]] | |
[["[email protected]" 73] | |
["[email protected]" 71]]) | |
;; #<HashSet [["Jane" 73], ["John" 71]]> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment