Skip to content

Instantly share code, notes, and snippets.

@handerpeder
Created September 24, 2022 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handerpeder/d6db931cd8fceca629ea4b42102d6f05 to your computer and use it in GitHub Desktop.
Save handerpeder/d6db931cd8fceca629ea4b42102d6f05 to your computer and use it in GitHub Desktop.
(require '[datomic.api :as d])
(def uri "datomic:mem://foo")
(d/delete-database uri)
(d/create-database uri)
(def conn (d/connect "datomic:mem://foo"))
(def test-schema [#:db{:ident :list/head,
:valueType :db.type/string,
:cardinality :db.cardinality/one,
:unique :db.unique/identity}
#:db{:ident :list/next,
:valueType :db.type/ref,
:cardinality :db.cardinality/one}
#:db{:ident :list/value,
:valueType :db.type/long,
:cardinality :db.cardinality/one,
:unique :db.unique/identity}])
@(d/transact conn test-schema)
(def some-values (for [i (range 100)]
{:list/value i}))
@(d/transact conn some-values)
(defn lv [{:keys [:list/value]}]
[:list/value value])
(->> (reduce (fn [[agg prev] current]
[(if-not prev
(conj agg [:db/add (lv current) :list/head "the-head"])
(conj agg [:db/add (lv prev) :list/next (lv current)]))
current])
[[] nil] (vec some-values))
first
(d/transact conn)
deref)
(def list-head
'[[(list-head [?x] ?head)
[?x :list/head]
[(identity ?x) ?head]]
[(list-head [?x] ?head)
[?y :list/next ?x]
[list-head ?y ?head]]])
(time
(d/q '[:find ?head
:in $ % ?value
:where
[?head :list/head]
[?elem :list/value ?value]
[list-head ?elem ?head]]
(d/db conn)
list-head
99))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment