This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-10-11
Channels
- # announcements (1)
- # babashka (132)
- # beginners (52)
- # calva (46)
- # clj-kondo (8)
- # cljdoc (17)
- # clojure (13)
- # clojure-australia (1)
- # clojure-dev (3)
- # clojure-europe (4)
- # clojurescript (4)
- # cloverage (1)
- # conjure (22)
- # datomic (9)
- # emacs (2)
- # fulcro (16)
- # leiningen (5)
- # malli (26)
- # off-topic (16)
- # pathom (3)
- # portal (5)
- # reagent (10)
- # reitit (5)
- # rewrite-clj (1)
- # ring (1)
- # shadow-cljs (14)
- # spacemacs (6)
- # tools-deps (10)
- # vim (11)
- # vscode (1)
- # xtdb (10)
i'm curious if anyone here makes use of recursion in their queries? i tend to find myself thinking "ah, recursion can solve this problem!" but then later i find myself implementing some tricky manipulations outside of the query to get back the results i want. for example, if i want to find the top level post of a nested post, then i have to walk the resulting tree to its maximum depth, which of course is pretty quick, but does not feel elegant.
; the "Third Level" post
(d/q '{:find [(pull ?post [:post/id :post/text {:post/_posts ...}])]
:in [$]
:where [[?post :post/id #uuid"40b8151d-d5f4-45a6-b78c-67655cdf1583"]]}
db)
=>
; and the top post being the most nested
[[{:post/id #uuid"40b8151d-d5f4-45a6-b78c-67655cdf1583",
:post/text "Third Level",
:post/_posts {:post/id #uuid"9209c1c6-d553-4632-848a-d9929fd7652a",
:post/text "Second Level",
:post/_posts {:post/id #uuid"0b15be5d-84f2-45d1-8b44-b9928d67f388",
:post/text "Top Level"}}}]]
you can also implement recursion with rules: https://docs.datomic.com/cloud/query/query-data-reference.html#rules
it might be easier to write a rule to get the id of the nested post, and then do a pull
on that id
hmm. i'm not sure how i would write a recursive rule though, e.g. one that unifies on a top level post in the example above
then again i haven't given it much thought. i'll play around and see what i can come up with. thanks for the advice 🙂