This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-21
Channels
- # beginners (5)
- # boot (15)
- # capetown (1)
- # chestnut (2)
- # cljs-dev (9)
- # cljsjs (3)
- # cljsrn (1)
- # clojure (190)
- # clojure-brasil (2)
- # clojure-greece (14)
- # clojure-italy (3)
- # clojure-poland (8)
- # clojure-romania (1)
- # clojure-russia (2)
- # clojure-serbia (3)
- # clojure-spec (38)
- # clojure-uk (98)
- # clojure-ukraine (2)
- # clojurescript (65)
- # clojurex (1)
- # core-async (16)
- # cursive (16)
- # datomic (3)
- # defnpodcast (7)
- # emacs (11)
- # funcool (2)
- # hoplon (16)
- # jobs (1)
- # leiningen (4)
- # lumo (9)
- # off-topic (2)
- # om (1)
- # other-languages (1)
- # protorepl (1)
- # re-frame (50)
- # reagent (16)
- # reitit (32)
- # remote-jobs (1)
- # rum (1)
- # shadow-cljs (73)
- # spacemacs (36)
- # specter (21)
- # sql (6)
- # unrepl (107)
- # untangled (4)
Say, anybody got a "recursively touch entity e
to depth n
" function lying around? I saw this but I need to handle the {:db/valueType :db.type/ref :db/cardinality :db.cardinality/many}
case. https://stackoverflow.com/questions/27072840/how-to-write-touch-all-touch-all-reachable-entities-from-an-entity-in-datomic
Well, this is what I came up with, not the prettiest or most efficient, but it works well enough for debugging purposes.
(defn touch-n
"Recursively touch an entity to n levels deep (default 2). Return a nested map."
([e]
(touch-n 2 e))
([n e]
(if (or (zero? n) (not (entity? e)))
;; We've reached our recursion limit, or we're at a leaf node
e
;; Else we're an an entity, touch it an recur
(reduce (fn [m [k v]]
(assoc m k (if (set? v)
;; Cardinality many
(into #{} (map (partial touch-n (dec n)) v))
;; Cardinality one
(touch-n (dec n) v))))
{}
(d/touch e)))))
(where I've already got (defn entity? [thing] (instance? datomic.Entity thing))
)