Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect clojure core.logic to a database?

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?

like image 737
M Smith Avatar asked Jan 25 '13 20:01

M Smith


1 Answers

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)))
like image 73
dnolen Avatar answered Nov 02 '22 04:11

dnolen