Query Question: How to calculate the ratio: a kind of row / all rows?

28 views
Skip to first unread message

Oskar

unread,
Nov 28, 2012, 3:00:42 AM11/28/12
to cascal...@googlegroups.com
Hi!

Say I have the words 

(def data ["bird" "cat" "whale" "elephant"])

and I want to calculate the ratio: words that has <= 4 letters / all words. The sort of closest I get is:

(?<- (stdout) [?short ?count] (data ?word) (letters-4-or-more? ?word :> ?short) (c/count ?count))

(If that would actually work, there is some problem that I posted about in another thread named "count breaks my queries".)

But is there a way to actually compute the ratio right there in the query?

Oleksandr Petrov

unread,
Nov 28, 2012, 3:46:40 AM11/28/12
to cascal...@googlegroups.com
Just ran the query:

(?<- (stdout) [?count] (data ?word) (cascalog.ops/count ?count))

RESULTS
-----------------------
4
-----------------------

Works absolutely fine. Check your letters-4-or-more function, and setup generally. 

--
alex p

Oskar

unread,
Nov 28, 2012, 4:22:09 AM11/28/12
to cascal...@googlegroups.com
I made another thread for my problem with the query not working, and in it there is no letters-4-or-more function, and there is also a stack trace. Maybe you just posted in the wrong one. Anyway, in this thread I was asking for a way to compute the ratio. Even my query had worked, it does not compute the ratio.

Oleksandr Petrov

unread,
Nov 28, 2012, 5:32:47 AM11/28/12
to cascal...@googlegroups.com
Once again, bare count works. I have posted example with result.

To calculate ratio, read up on custom operations. https://github.com/nathanmarz/cascalog/wiki/Guide-to-custom-operations there're several ways to do it. Aggregators section should help you.

Calculate ratio is just a matter of division of 2 things, so is no brainer. 
What's important is how to count them. 

--
alex p

Andy Xue

unread,
Nov 28, 2012, 11:56:32 AM11/28/12
to cascal...@googlegroups.com
yep alex is right, you need to figure out a condition count, ie, "count if" type of aggregate op
Reply all
Reply to author
Forward
0 new messages