datahike

https://datahike.io/, Join the conversation at https://discord.com/invite/kEBzMvb, history for this channel is available at https://clojurians.zulipchat.com/#narrow/stream/180378-slack-archive/topic/datahike
Björn Ebbinghaus 2020-07-19T17:43:22.011200Z

Can anybody help me with a query for a count by two categories? I have a set of opinions. Opinions have the value -1,0 or 1. I want the count of all 1 opinions and the count of all -1 opinions. So far I have:

[:find [(count ?pros) (count ?cons)]
    :in $ ?id
    :where
    [?e ::proposal/id ?id]  
    [?e ::proposal/opinions ?pros]  ; 1
    [?pros ::opinion/value 1])      ; 2
    [?e ::proposal/opinions ?cons]  ; 3
    [?cons ::opinion/value -1]))]   ; 4
This won’t work, because in this query ?pros and ?cons would be the same, and 2 and 4 would contradict each other. I think I do not understand or-join enough to say if I have to use it.

Björn Ebbinghaus 2020-07-20T15:03:38.022800Z

@ben.sless Thanks a lot! The final query:

(d/q 
  '[:find (clojure.core/frequencies ?values) .
    :where
    [?e ::proposal/id]
    [?e ::proposal/opinions ?opinions]
    [?opinions ::opinion/value ?values]]
  @db/conn)
@whilo I cannot use frequencies without clojure.core . I get:
Execution error (NullPointerException) at datahike.query/-aggregate$fn (query.cljc:907).
null
It seams that Datomic doesn’t need the namespace,

Ben Sless 2020-07-20T15:13:47.023200Z

to be fair, the datahike docs say you need to provide a fully qualified name. still a gap relative to datomic

Björn Ebbinghaus 2020-07-20T15:17:39.023400Z

Ah. okay. Didn’t see that one.

Ben Sless 2020-07-20T15:23:30.023600Z

Don't feel bad 🙂 custom aggregators aren't something you'd expect to find on the front page

Björn Ebbinghaus 2020-07-19T17:46:23.012500Z

(I like asking questions in Slack because of the personal conversations, but Stack Overflow would be better because other people can find and reference the question later)

Ben Sless 2020-07-19T20:25:51.012600Z

Write your own aggregator which will behave like frequencies?

Ben Sless 2020-07-19T20:29:49.012800Z

for example this is how max aggregator is implemented:

(fn
  [coll]
  (reduce (fn [acc x]
            (if (pos? (compare x acc))
              x acc))
          (first coll) (next coll)))

Ben Sless 2020-07-19T20:30:43.013Z

I think you can even just use frequencies directly