Created
May 25, 2013 23:00
-
-
Save a2ndrade/5651065 to your computer and use it in GitHub Desktop.
Datomic: memoized predicate functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; see http://stackoverflow.com/questions/16386793/datomic-query-function-that-filters-and-binds | |
(require '[datomic.api :as d]) | |
(def uri "datomic:mem://test") | |
(d/create-database uri) | |
(def conn (d/connect uri)) | |
;; sample attribute definiton | |
(d/transact conn [{:db.install/_attribute :db.part/db | |
:db/id #db/id[:db.part/db] | |
:db/ident :user/regex-match | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one}]) | |
;; sample data | |
(d/transact conn [{:db/id #db/id[:db.part/user] :user/regex-match "so 1"} | |
{:db/id #db/id[:db.part/user] :user/regex-match "no-match"} | |
{:db/id #db/id[:db.part/user] :user/regex-match "so 23"} | |
{:db/id #db/id[:db.part/user] :user/regex-match "no-match 2"} | |
{:db/id #db/id[:db.part/user] :user/regex-match "so 456"}]) | |
(defn match? [m] (re-find (re-pattern "so (\\d+)") m)) | |
(def fast-match? (memoize match?)) | |
(->> (d/datoms (d/db conn) :aevt :user/regex-match) | |
(filter #(fast-match? (:v %))) | |
(map :v)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment