This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-05-05
Channels
- # announcements (7)
- # babashka (20)
- # beginners (130)
- # bristol-clojurians (1)
- # cider (14)
- # clj-kondo (7)
- # cljdoc (14)
- # cljs-dev (15)
- # cljsrn (16)
- # clojars (11)
- # clojure (190)
- # clojure-dev (4)
- # clojure-europe (7)
- # clojure-italy (9)
- # clojure-nl (3)
- # clojure-romania (6)
- # clojure-uk (51)
- # clojurescript (44)
- # component (4)
- # conjure (28)
- # cursive (1)
- # data-science (4)
- # datascript (1)
- # datomic (30)
- # duct (4)
- # emacs (1)
- # figwheel (4)
- # fulcro (56)
- # graalvm (4)
- # helix (51)
- # jackdaw (2)
- # jobs-discuss (12)
- # joker (4)
- # lambdaisland (1)
- # local-first-clojure (1)
- # meander (73)
- # mid-cities-meetup (2)
- # nrepl (4)
- # off-topic (43)
- # pathom (56)
- # re-frame (37)
- # reagent (26)
- # shadow-cljs (161)
- # slack-help (9)
- # spacemacs (1)
- # tools-deps (18)
- # xtdb (18)
Hi, may be a stupid question but what is the best way to count with crux ? I had a look at the crux.decorators but not sure what should be done ? thanks
Hey - it's a good question! Are you looking to count result tuples in general? Or, more specifically, count entities with certain attributes? Or are you looking for counts to be returned within tuples? I think there may be a few different options.
Hi Jeremy, more count entities with certain attributes even if i don't see the difference between the first and the second proposition ?
Okay, it's good to know the context. I think crux.decorators could definitely help you, but as you only want to do counts (for now) it would be simplest to just reduce over the results in your own code to create a count.
Here is what i've done for now
(defn count-translations-by-status
[]
(let [all (crux/q
(crux/db node)
{:find '[?id ?s]
:where '[[?e :crux.db/id ?id]
[?e :app/type :app/Translation]
[?e :translation/status ?s]]})
gp (group-by last all)
gp (zipmap (keys gp) (map #(count (second %)) gp))]
gp))
So that looks like a reasonable solution to me. There are probably some small efficiency tweaks that can be made but I nothing springs to mind.
If you have a very large result set then you could use open-q
to do some of that processing more lazily.
I wouldn't recommend trying to adopt crux.decorators.aggregation.alpha
right now, as it's not under active maintenance, although it would certainly be a more pleasant solution if you have aggregation logic to maintain in many different places. We are hoping to review and bring it up-to-date again soon though.
given you've not got many documents here this'll be fine - if memory was a concern you could use clojure.core/frequencies
- (->> all (map last) (frequencies))
(->> [[:id1 :DRAFT]
[:id2 :PUBLISHED]
[:id3 :DRAFT]
[:id4 :WAITING-FOR-APPROVAL]]
(map last)
(frequencies))
;; => {:DRAFT 2, :PUBLISHED 1, :WAITING-FOR-APPROVAL 1}
got pull to work with crux by making some minor edits to the datascript pull implementation: https://gist.github.com/dvingo/ca637c159ff58c54d320054c98368e0e everything works (defaults, limits, recursion - limited and unlimited, prop renaming, prop wildcard) except reverse attribute lookup
hey @U051V5LLP, looking good - thanks! 🙂
was hacking on something similar myself on Friday afternoon, using the EQL library - seems like it really brings the implementation size/complexity down
for sure! 🙂 i was playing around with using eql as well, luckily i somehow had the thought to see how datascript did it, ha.
This is a nice find - where would the world be without datascript! Reverse lookups for Crux will certainly be a bit different in the sense that everything gets returned as if it was cardinality-many. Will you need to solve it for your data model anyway, or is your gist enough for you?