Skip to content

Instantly share code, notes, and snippets.

@micha
Created March 17, 2014 15:45
Show Gist options
  • Save micha/9601749 to your computer and use it in GitHub Desktop.
Save micha/9601749 to your computer and use it in GitHub Desktop.
Datomic transactor function that works like Clojure's `swap!` function.
{:db/id #db/id[:db.part/db]
:db/doc "Works like clojure.core/swap!, basically."
:db/ident :swap!
:db/fn #db/fn {:lang "clojure"
:params [db id k expr args]
:code "(let [f (if-not (string? expr)
(resolve expr)
(eval (read-string expr)))
e (datomic.api/entity db id)
v (apply f (concat [(get e k 0)] args))]
[[:db/add id k v]])"}}
@micha
Copy link
Author

micha commented May 17, 2014

@(d/transact conn [[:swap! 123456 :foo/count '+ [42]]])
@(d/transact conn [[:swap! 123456 :foo/count "(fn [x y] (+ x y))" [42]]])

@micha
Copy link
Author

micha commented May 17, 2014

Possibly want eval on line 7 instead of resolve?

Perhaps to be able to do this:

@(d/transact conn [[:swap! 123456 :foo/count '(fn [x y] (+ x y)) [42]]])

I wonder if that would work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment