This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-13
Channels
- # aleph (7)
- # announcements (3)
- # babashka (29)
- # beginners (70)
- # calva (5)
- # cider (14)
- # clara (3)
- # clj-kondo (25)
- # cljs-dev (2)
- # clojure (237)
- # clojure-conj (3)
- # clojure-europe (6)
- # clojure-italy (14)
- # clojure-nl (4)
- # clojure-uk (40)
- # clojurescript (29)
- # clojurex (1)
- # code-reviews (2)
- # cursive (3)
- # datascript (1)
- # fulcro (11)
- # graalvm (4)
- # graphql (12)
- # jackdaw (1)
- # jobs (1)
- # joker (22)
- # london-clojurians (1)
- # off-topic (132)
- # re-frame (38)
- # rewrite-clj (11)
- # shadow-cljs (200)
- # spacemacs (1)
- # sql (67)
- # tools-deps (15)
I’m trying to figure out if there is a way to accumulate some joined data. I haven’t been able to come up with a single rule which will fire with my desired output.
In the following example, I am trying to answer the question, what is the sum of quantities
for each name
:
(defrecord A [id name])
(defrecord B [id quantity])
(defrule merge-things
???
=>
(println ?name ?total-quanity))
(-> (mk-session)
(insert (->A 1 "car"))
(insert (->A 2 "truck"))
(insert (->A 3 "car"))
(insert (->B 1 2))
(insert (->B 2 3))
(insert (->B 3 5))
(fire-rules))
I am trying to have that rule print out:
car 7
truck 3
Well, to the rescue. While typing this up, I found one way I hadn’t tried. So now my question is, are there any problems with the following rule definition? (memory,performance,other,…)
It is different that other rules I’ve used. Not sure if using the accumulator and re-creating the set of ids — (set (map :id ?a))
— in the rule expression would cause any unexpected problems.
(defrule merge-things
[?a <- (acc/all) :from [A (= ?name name)]]
[?total-quantity <- (acc/sum :quantity) :from [B (get (set (map :id ?a)) id)]]
=>
(println ?name ?total-quantity))