Skip to content

Instantly share code, notes, and snippets.

@shaunr0b
Last active August 29, 2015 13:57
Show Gist options
  • Save shaunr0b/9768695 to your computer and use it in GitHub Desktop.
Save shaunr0b/9768695 to your computer and use it in GitHub Desktop.
Snippet that registers a Datomic attribute with function valueType; then create an enitity with a function. Next, retrieve the function via a query, bind it to a var, and invoke it using params.
(require '[datomic.api :as d])
(def uri "datomic:mem://hello")
(d/create-database uri)
;; Define a transaction to create an attribute with type function (:some/fn)
(def attribute-tx [ {:db/id (d/tempid :db.part/db)
:db/ident :regular/attribute
:db/cardinality :db.cardinality/one
:db/valueType :db.type/fn
:db.install/_attribute :db.part/db} ])
;; Define a transaction to create a function
(def fn-tx [{:db/id (d/tempid :db.part/user)
:regular/attribute #db/fn {:lang :clojure
:params [name]
:code (str "Hello, " name)}}])
;; Transact
(d/transact (d/connect uri) attribute-tx)
(d/transact (d/connect uri) fn-tx)
;; Query and bind result to var
(def hello (ffirst (d/q '[:find ?v
:where
[?e :regular/attribute ?v]]
(d/db (d/connect uri)))))
;; Invoke function with params
(hello "Jordan!")
;; => "Hello, Jordan!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment