复杂的约简样本不清楚约简的工作原理

时间:2018-09-06 04:04:38

标签: crossfilter

complex reduce sample开始 我已将其缩小为一张图表,并试图了解减少的作用方式

我已经在代码中添加了注释,而示例中没有注释,表明我认为是基于我阅读文档的方式。

function groupArrayAdd(keyfn) {
      var bisect = d3.bisector(keyfn);  //set the bisector value function
     //elements is the group that we are reducing,item is the current item
       //this is a the reduce function being supplied to the reduce call on the group runAvgGroup for add below
      return function(elements, item) {
          //get the position of the key value  for this element in the sorted array and put it there
          var pos = bisect.right(elements, keyfn(item));
          elements.splice(pos, 0, item);
          return elements;
      };
  }
  function groupArrayRemove(keyfn) {
      var bisect = d3.bisector(keyfn);//set the bisector value function
    //elements is the group that we are reducing,item is the current item
    //this is a the reduce function being supplied to the reduce call on the group runAvgGroup for remove below
      return function(elements, item) {
        //get the position of the key value  for this element in the sorted array and splice it out
          var pos = bisect.left(elements, keyfn(item));
          if(keyfn(elements[pos])===keyfn(item))
              elements.splice(pos, 1);
          return elements;
      };
  }
  function groupArrayInit() {
                  //for each key found by the key function return this array?
      return [];  //the result array for where the data is being inserted in sorted order?
  }

我不太确定我对此的理解是否正确。有些魔力没有显现出来。我是否正确说元素是正在调用reduce函数的组?还有groupArrayInit()中的数组是如何间接填充的?

我的一部分感觉到,提供给reduce调用的函数实际上是array.map函数而不是array.reduce函数,但是我只是不太清楚为什么。阅读文档后,我只是不在这里建立连接。

任何帮助将不胜感激。 我是否也错过了为所有这些示例创建的钢笔/小提琴?像这个 http://dc-js.github.io/dc.js/examples/complex-reduce.html是我从这里开始的地方,但必须下载csv并手动将其转换为Json。

--------------更新 我添加了一些打印语句以尝试阐明add函数的工作方式

  function groupArrayAdd(keyfn) {
      var bisect = d3.bisector(keyfn);  //set the bisector value function
     //elements is the group that we are reducing,item is the current item
       //this is a the reduce function being supplied to the reduce call on the group runAvgGroup for add below
      return function(elements, item) {
        console.log("---Start Elements and Item and keyfn(item)----")
        console.log(elements)  //elements grouped by run?
        console.log(item)   //not seeing the pattern on what this is on each run
        console.log(keyfn(item))
        console.log("---End----")
          //get the position of the key value  for this element in the sorted array and put it there
          var pos = bisect.right(elements, keyfn(item));
          elements.splice(pos, 0, item);
          return elements;
      };
  }

并打印组的内容

console.log("RunAvgGroup")
console.log(runAvgGroup.top(Infinity))

这导致 final group not sorted by Key

哪些似乎不正确b / c值未按键(运行编号)排序? 而且,查看打印语句的结果似乎也无济于事。

1 个答案:

答案 0 :(得分:1)

这对我来说基本上是对的。这些问题只是概念性的。

交叉过滤器的group.reduce与Array.reduce或Array.map不完全相同。 Group.reduce定义用于处理向组中添加新记录或从组中删除记录的方法。因此,它在概念上类似于支持反转操作的增量Array.reduce。这允许应用和删除过滤器。

Group.top返回您的组列表。这些组的value属性应该是reduce函数返回的元素值。组的键是您的组访问器(在创建组的Dimension.group调用中定义)或您未定义组访问器的维访问器返回的值。 Reduce函数仅对组值起作用,而不能直接访问组键。

因此,请在group.top输出中检查这些值,并希望您会看到期望的元素列表。