I have been enjoying Clojure core.logic but I have run into a wall. I need to be able to have it use a database, either SQL or not, rather than an in memory data structure. As I have looked around I see mention of a to-stream
function but no solid examples of its use.
Does anyone have a good example of using core.logic with a database?
As someone has already suggested in the comments look at the Datomic example in the core.logic repository. Based on the example there you could imagine writing something like this:
(defn query [db query-string out]
(fn [a]
(to-stream
(map (fn [result] (unify a out result))
(db-query db query-string)))))
All core.logic goals just return closures which take a substitution map a
(you can of course call it whatever you like). Essentially you need to map over the results and unify them with out
in a
.
Then you could imagine writing a core.logic program like the following:
(run* [q]
(fresh [row]
(query some-db "... some query string ..." row)
(some-other-goal row q)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With