javascript 交叉过滤器中的 reduceAdd、reduceSum、reduceRemove 函数是什么?它们应该如何使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16767231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:54:36  来源:igfitidea点击:

What are the reduceAdd, reduceSum , reduceRemove functions in crossfilter? How should they be used?

javascriptcrossfilter

提问by Navya R

Can someone explain in simple terms how reduce function with its arguments reduceAdd, reduceSum, reduceRemoveworks in crossfilter?

简单来说有人能解释如何减少功能与它的参数reduceAddreduceSumreduceRemove的作品crossfilter

回答by Isioma Nnodum

Remember that map reduce reduces a dataset by keys of a particular dimension. For example lets use a crossfilter instance with records:

请记住,map reduce 通过特定维度的键来减少数据集。例如,让我们使用带有记录的 crossfilter 实例:

[
    { name: "Gates",      age: 57,   worth: 72000000000, gender: "m" },
    { name: "Buffet",     age: 59,   worth: 58000000000, gender: "m" },
    { name: "Winfrey",    age: 83,   worth:  2900000000, gender: "f"   },
    { name: "Bloomberg",  age: 71,   worth: 31000000000, gender: "m"  },
    { name: "Walton",     age: 64,   worth: 33000000000, gender: "f"  },
]

and dimensions name, age, worth, and gender. We will reduce the gender dimension using the reduce method.

和维度名称、年龄、价值和性别。我们将使用reduce方法来减少性别维度。

First we define the reduceAdd, reduceRemove, and reduceInitial callback methods.

首先我们定义reduceAdd、reduceRemove 和reduceInitial 回调方法。

reduceInitialreturns an object with the form of the reduced object and the initial values. It takes no parameters.

reduceInitial返回具有缩减对象形式和初始值的对象。它不需要参数。

function reduceInitial() {
    return {
        worth: 0,
        count: 0
    };
}

reduceAdddefines what happens when a record is being 'filtered into' the reduced object for a particular key. The first parameter is a transient instance of the reduced object. The second object is the current record. The method will return the augmented transient reduced object.

reduceAdd定义当记录被“过滤到”特定键的缩减对象时会发生什么。第一个参数是缩减对象的瞬态实例。第二个对象是当前记录。该方法将返回增强的瞬态减少对象。

function reduceAdd(p, v) {
    p.worth = p.worth + v.worth;
    p.count = p.count + 1;
    return p;
}

reduceRemovedoes the opposite of reduceAdd(at least in this example). It takes the same parameters as reduceAdd. It is needed because group reduces are updated as records are filtered and sometimes records need to be removed from a previously computed group reduction.

reduceRemovereduceAdd(至少在这个例子中)相反。它采用与 相同的参数reduceAdd。之所以需要它,是因为在过滤记录时会更新组减少,有时需要从先前计算的组减少中删除记录。

function reduceRemove(p, v) {
    p.worth = p.worth - v.worth;
    p.count = p.count - 1;
    return p;
}

Invoking the reduce method would look like this:

调用reduce方法看起来像这样:

mycf.dimensions.gender.reduce(reduceAdd, reduceRemove, reduceInitial)

To take a peek at the reduced values, use the allmethod. To see the top n values use the top(n)method.

要查看减少的值,请使用该all方法。要查看前 n 个值,请使用该top(n)方法。

mycf.dimensions.gender.reduce(reduceAdd, reduceRemove, reduceInitial).all()

The returned array would (should) look like:

返回的数组将(应该)如下所示:

[
    { key: "m", value: { worth: 161000000000, count: 3 } },
    { key: "f", value: { worth:  35000000000, count: 2 } },
]

The goals of reducing a dataset is to derive a new dataset by first grouping records by common keys, then reducing a dimension those groupings into a single value for each key. In this case we grouped by gender and reduced the worth dimension of that grouping by adding the values of records that shared the same key.

减少数据集的目标是通过首先按公共键对记录进行分组来派生新数据集,然后将这些分组的维度减少为每个键的单个值。在这种情况下,我们按性别分组,并通过添加共享相同键的记录的值来减少该分组的价值维度。

The other reduceX methods are convience methods for the reduce method.

其他reduceX 方法是reduce 方法的便捷方法。

For this example reduceSumwould be the most appropriate replacement.

对于这个例子reduceSum将是最合适的替代品。

mycf.dimensions.gender.reduceSum(function(d) {
    return d.worth;
});

Invoking allon the returned grouping would (should) look like:

调用all返回的分组将(应该)如下所示:

[
    { key: "m", value: 161000000000 },
    { key: "f", value: 35000000000 },
]

reduceCountwill count records

reduceCount会统计记录

mycf.dimensions.gender.reduceCount();

Invoking allon the returned grouping would (should) look like:

调用all返回的分组将(应该)如下所示:

[
    { key: "m", value: 3 },
    { key: "f", value: 2 },
]

Hope this helps :)

希望这可以帮助 :)

Source: https://github.com/square/crossfilter/wiki/API-Reference

来源:https: //github.com/square/crossfilter/wiki/API-Reference

回答by Navya R

http://blog.rusty.io/2012/09/17/crossfilter-tutorial/

http://blog.rusty.io/2012/09/17/crossfilter-tutorial/

var livingThings = crossfilter([
  // Fact data.
  { name: “Rusty”,  type: “human”, legs: 2 },
  { name: “Alex”,   type: “human”, legs: 2 },
  { name: “Lassie”, type: “dog”,   legs: 4 },
  { name: “Spot”,   type: “dog”,   legs: 4 },
  { name: “Polly”,  type: “bird”,  legs: 2 },
  { name: “Fiona”,  type: “plant”, legs: 0 }
]);

For example, how many living things are in my house?

例如,我家有多少生物?

To do this, we'll call the groupAllconvenience function, which selects all records into a single group, and then the reduceCountfunction, which creates a count of the records.

为此,我们将调用groupAll便利函数,该函数将所有记录选择到一个组中,然后reduceCount调用创建记录计数的函数。

// How many living things are in my house?
var n = livingThings.groupAll().reduceCount().value();
console.log("There are " + n + " living things in my house.") // 6

Now let's get a count of all the legs in my house. Again, we'll use the groupAllfunction to get all records in a single group, but then we call the reduceSumfunction. This is going to sum values together. What values? Well, we want legs, so let's pass a function that extracts and returns the number of legs from the fact.

现在让我们数一数我家里所有的腿。同样,我们将使用该groupAll函数获取单个组中的所有记录,但随后我们将调用该 reduceSum函数。这将把值加在一起。什么价值观?好吧,我们想要腿,所以让我们传递一个函数,从事实中提取并返回腿的数量。

// How many total legs are in my house?
var legs = livingThings.groupAll().reduceSum(function(fact) {
  return fact.legs;
}).value()
console.log("There are " + legs + " legs in my house.")

reduceCountfunction creates a count of the records.
reduceSumfunction is the sum values of these records.

reduceCount函数创建记录计数。
reduceSum函数是这些记录的总和值。