This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-07
Channels
- # aleph (5)
- # announcements (2)
- # babashka (17)
- # beginners (13)
- # cider (4)
- # clj-kondo (9)
- # cljdoc (18)
- # clojure (20)
- # clojure-art (7)
- # clojure-dev (11)
- # clojure-europe (20)
- # clojure-nl (2)
- # clojure-norway (53)
- # clojure-spec (30)
- # clojure-uk (4)
- # clojurescript (40)
- # datomic (70)
- # events (2)
- # graalvm-mobile (8)
- # gratitude (2)
- # guix (2)
- # honeysql (3)
- # hyperfiddle (10)
- # introduce-yourself (4)
- # jobs (2)
- # lsp (6)
- # luminus (3)
- # malli (5)
- # polylith (35)
- # practicalli (6)
- # re-frame (3)
- # reagent (9)
- # releases (1)
- # remote-jobs (20)
- # ring-swagger (2)
- # shadow-cljs (12)
- # sql (18)
- # xtdb (7)
Is it possible to parameterize the predicate functions used in queries? I have a pretty large query (100+ lines with multiple subqueries) that will only differ by one custom predicate function, for several different use cases. Ideally I wouldn’t have to maintain 3-4 identical copies of this large query. Example:
(defn my-q [node predicate]
(xt/q (xt/db node)
{:find '[a]
:where ['[a :some-key ?b]
[(predicate '?b)]]}))
(my-q my-node 'lib/predicate-1)
(my-q my-node 'lib/predicate-2)
(my-q my-node 'lib/predicate-3)
hey @U03DWT00WUB 👋 try a list
call?
(defn my-q [node predicate]
(xt/q (xt/db node)
{:find '[a]
:where ['[a :some-key ?b]
[(list predicate '?b)]]}))
That worked! Do you know why that works specifically? I didn’t see anything like that in the docs.
yep, exactly that - XT queries are just data structures, so you could temporarily check what you're passing either with a doto-prn or even just returning the data structure instead, checking it at the REPL (as you were above) then putting it back:
(defn my-q [node predicate]
(xt/q (xt/db node)
(doto {:find '[a]
:where ['[a :some-key ?b]
[(list predicate '?b)]]}
prn)))
(defn my-q [node predicate]
{:find '[a]
:where ['[a :some-key ?b]
[(list predicate '?b)]]})