Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created August 27, 2012 09:11
Show Gist options
  • Save martintrojer/3486837 to your computer and use it in GitHub Desktop.
Save martintrojer/3486837 to your computer and use it in GitHub Desktop.
Datalog blog
(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 {})))
(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"})
(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