update reduce examples, closes influxdata/DAR#249 (#3446)
parent
f0cd5390f8
commit
86f67a8e50
|
@ -27,8 +27,8 @@ _`reduce()` is an [aggregate function](/flux/v0.x/function-types/#aggregates)._
|
|||
|
||||
```js
|
||||
reduce(
|
||||
fn: (r, accumulator) => ({ sum: r._value + accumulator.sum }),
|
||||
identity: {sum: 0.0}
|
||||
fn: (r, accumulator) => ({ sum: r._value + accumulator.sum }),
|
||||
identity: {sum: 0.0}
|
||||
)
|
||||
```
|
||||
|
||||
|
@ -62,10 +62,10 @@ If the output record keys and value types do not match the `identity` keys and v
|
|||
it will return a type error.
|
||||
{{% /note %}}
|
||||
|
||||
#### r
|
||||
#### r {data-type=record}
|
||||
Record representing each row or record.
|
||||
|
||||
#### accumulator
|
||||
#### accumulator {data-type=record}
|
||||
Reducer record defined by [`identity`](#identity).
|
||||
|
||||
### identity {data-type="record"}
|
||||
|
@ -108,9 +108,7 @@ import "sampledata"
|
|||
|
||||
sampledata.int()
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
sum: r._value + accumulator.sum
|
||||
}),
|
||||
fn: (r, accumulator) => ({sum: r._value + accumulator.sum}),
|
||||
identity: {sum: 0}
|
||||
)
|
||||
```
|
||||
|
@ -147,8 +145,8 @@ import "sampledata"
|
|||
sampledata.int()
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
sum: r._value + accumulator.sum,
|
||||
count: accumulator.count + 1
|
||||
sum: r._value + accumulator.sum,
|
||||
count: accumulator.count + 1,
|
||||
}),
|
||||
identity: {sum: 0, count: 0}
|
||||
)
|
||||
|
@ -185,9 +183,7 @@ import "sampledata"
|
|||
|
||||
sampledata.int()
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
prod: r._value * accumulator.prod
|
||||
}),
|
||||
fn: (r, accumulator) => ({prod: r._value * accumulator.prod}),
|
||||
identity: {prod: 1}
|
||||
)
|
||||
```
|
||||
|
@ -222,13 +218,14 @@ sampledata.int()
|
|||
import "sampledata"
|
||||
|
||||
sampledata.int()
|
||||
|> reduce(fn: (r, accumulator) => ({
|
||||
count: accumulator.count + 1,
|
||||
total: accumulator.total + r._value,
|
||||
avg: float(v: (accumulator.total + r._value)) / float(v: accumulator.count)
|
||||
}),
|
||||
identity: {count: 1, total: 0, avg: 0.0}
|
||||
)
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
count: accumulator.count + 1,
|
||||
total: accumulator.total + r._value,
|
||||
avg: float(v: (accumulator.total + r._value)) / float(v: accumulator.count + 1)
|
||||
}),
|
||||
identity: {count: 0, total: 0, avg: 0.0}
|
||||
)
|
||||
```
|
||||
|
||||
{{< expand-wrapper >}}
|
||||
|
@ -245,11 +242,11 @@ sampledata.int()
|
|||
##### Output data
|
||||
| tag | avg | count | total |
|
||||
| :-- | --: | ----: | ----: |
|
||||
| t1 | 8.5 | 7 | 51 |
|
||||
| t1 | 8.5 | 6 | 51 |
|
||||
|
||||
| tag | avg | count | total |
|
||||
| :-- | ----------------: | ----: | ----: |
|
||||
| t2 | 8.834 | 7 | 53 |
|
||||
| tag | avg | count | total |
|
||||
| :-- | ----: | ----: | ----: |
|
||||
| t2 | 8.834 | 6 | 53 |
|
||||
|
||||
{{% /flex-content %}}
|
||||
{{< /flex >}}
|
||||
|
|
|
@ -159,23 +159,18 @@ does the same thing and is much more performant._
|
|||
{{% code-tab-content %}}
|
||||
|
||||
```js
|
||||
average = (tables=<-, outputField="average") =>
|
||||
tables
|
||||
average = (tables=<-, outputField="average") => tables
|
||||
|> reduce(
|
||||
// Define the initial accumulator record
|
||||
identity: {
|
||||
count: 1.0,
|
||||
sum: 0.0,
|
||||
avg: 0.0
|
||||
},
|
||||
fn: (r, accumulator) => ({
|
||||
// Increment the counter on each reduce loop
|
||||
count: accumulator.count + 1.0,
|
||||
// Add the _value to the existing sum
|
||||
sum: accumulator.sum + r._value,
|
||||
// Divide the existing sum by the existing count for a new average
|
||||
avg: accumulator.sum / accumulator.count
|
||||
})
|
||||
// Define the initial accumulator record
|
||||
identity: {count: 0.0, sum: 0.0, avg: 0.0},
|
||||
fn: (r, accumulator) => ({
|
||||
// Increment the counter on each reduce loop
|
||||
count: accumulator.count + 1.0,
|
||||
// Add the _value to the existing sum
|
||||
sum: accumulator.sum + r._value,
|
||||
// Divide the existing sum by the existing count for a new average
|
||||
avg: (accumulator.sum + r._value) / (accumulator.count + 1.0),
|
||||
}),
|
||||
)
|
||||
// Drop the sum and the count columns since they are no longer needed
|
||||
|> drop(columns: ["sum", "count"])
|
||||
|
@ -189,21 +184,16 @@ average = (tables=<-, outputField="average") =>
|
|||
|
||||
{{% code-tab-content %}}
|
||||
```js
|
||||
average = (tables=<-, outputField="average") =>
|
||||
tables
|
||||
average = (tables=<-, outputField="average") => tables
|
||||
|> reduce(
|
||||
identity: {
|
||||
count: 1.0,
|
||||
sum: 0.0,
|
||||
avg: 0.0
|
||||
},
|
||||
fn: (r, accumulator) => ({
|
||||
count: accumulator.count + 1.0,
|
||||
sum: accumulator.sum + r._value,
|
||||
avg: accumulator.sum / accumulator.count
|
||||
})
|
||||
identity: {count: 0.0, sum: 0.0, avg: 0.0},
|
||||
fn: (r, accumulator) => ({
|
||||
count: accumulator.count + 1.0,
|
||||
sum: accumulator.sum + r._value,
|
||||
avg: (accumulator.sum + r._value) / (accumulator.count + 1.0),
|
||||
}),
|
||||
)
|
||||
|> drop(columns: ["sum", "count"])
|
||||
|> drop(columns: ["sum", "count"])
|
||||
|> set(key: "_field", value: outputField)
|
||||
|> rename(columns: {avg: "_value"})
|
||||
```
|
||||
|
@ -218,23 +208,22 @@ The following function expects input tables to have `c1_value` and `c2_value`
|
|||
columns and generates an average for each.
|
||||
|
||||
```js
|
||||
multiAvg = (tables=<-) =>
|
||||
tables
|
||||
multiAvg = (tables=<-) => tables
|
||||
|> reduce(
|
||||
identity: {
|
||||
count: 1.0,
|
||||
c1_sum: 0.0,
|
||||
c1_avg: 0.0,
|
||||
c2_sum: 0.0,
|
||||
c2_avg: 0.0
|
||||
},
|
||||
fn: (r, accumulator) => ({
|
||||
count: accumulator.count + 1.0,
|
||||
c1_sum: accumulator.c1_sum + r.c1_value,
|
||||
c1_avg: accumulator.c1_sum / accumulator.count,
|
||||
c2_sum: accumulator.c2_sum + r.c2_value,
|
||||
c2_avg: accumulator.c2_sum / accumulator.count
|
||||
})
|
||||
identity: {
|
||||
count: 1.0,
|
||||
c1_sum: 0.0,
|
||||
c1_avg: 0.0,
|
||||
c2_sum: 0.0,
|
||||
c2_avg: 0.0,
|
||||
},
|
||||
fn: (r, accumulator) => ({
|
||||
count: accumulator.count + 1.0,
|
||||
c1_sum: accumulator.c1_sum + r.c1_value,
|
||||
c1_avg: accumulator.c1_sum / accumulator.count,
|
||||
c2_sum: accumulator.c2_sum + r.c2_value,
|
||||
c2_avg: accumulator.c2_sum / accumulator.count,
|
||||
}),
|
||||
)
|
||||
```
|
||||
|
||||
|
@ -243,16 +232,13 @@ Use `reduce()` to create a function that aggregates gross and net profit.
|
|||
This example expects `profit` and `expenses` columns in the input tables.
|
||||
|
||||
```js
|
||||
profitSummary = (tables=<-) =>
|
||||
tables
|
||||
profitSummary = (tables=<-) => tables
|
||||
|> reduce(
|
||||
identity: {
|
||||
gross: 0.0,
|
||||
net: 0.0
|
||||
},
|
||||
fn: (r, accumulator) => ({
|
||||
gross: accumulator.gross + r.profit,
|
||||
net: accumulator.net + r.profit - r.expenses
|
||||
})
|
||||
identity: {gross: 0.0, net: 0.0},
|
||||
fn: (r, accumulator) => ({
|
||||
gross: accumulator.gross + r.profit,
|
||||
net: accumulator.net + r.profit - r.expenses
|
||||
}
|
||||
)
|
||||
)
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue