2014-11-16 38 views
1

From https://github.com/tonsky/datascript为什么这个数据库查询是聚合的?

(-> 
(d/q '[:find ?color (max ?amount ?x) (min ?amount ?x) 
     :in [[?color ?x]] ?amount] 
     [[:red 10] [:red 20] [:red 30] [:red 40] [:red 50] 
     [:blue 7] [:blue 8]] 
     4) 
pr-str 
js/console.log) 
;;; ([:red [20 30 40 50] [10 20 30 40]] [:blue [7 8] [7 8]]) 

(-> 
(d/q '[:find ?color (max ?amount ?x) (min ?amount ?x) 
     :in [[?color ?x]] ?amount] 
     [[:red 10] [:red 20] [:red 30] [:red 40] [:red 50] 
     [:blue 7] [:blue 8]] 
     3) 
pr-str 
js/console.log) 
;;; ([:red [30 40 50] [10 20 30]] [:blue [7 8] [7 8]]) 

(-> 
(d/q '[:find ?color (max ?amount ?x) (min ?amount ?x) 
     :in [[?color ?x]] ?amount] 
     [[:red 10] [:red 20] [:red 30] [:red 40] [:red 50] 
     [:blue 7] [:blue 8]] 
     2) 
pr-str 
js/console.log) 
;;; ([:red [40 50] [10 20]] [:blue [7 8] [7 8]]) 

所以,这不是一个关于它是做什么的问题,这是它是如何(或者至少是为什么)做的问题。 max和min分别是返回其后续整数的最大值或最小值的函数。 ?amount如何计算限制聚合计数?为什么这些东西聚合呢?代码如何运行以便聚合。我真的没有看到这个代码是如何产生它所做的结果的。

回答

2

maxminoverloaded在数据组查询。

一元(min ?x)(max ?x)功能集合返回一个单一的数字。

二进制(min ?n ?x)(max ?n ?x)函数会聚合以返回长度限制为?n的项集合。

+0

澄清:(max N coll)是coll的前N个值。同样的分钟,这是N最低的科尔。 –

相关问题