Data Pipeline Patterns
Reduce Total
Reduction carries one accumulator through every input value.
Program
Play the program to sum amounts and optionally add shipping.
reduce_total.dart
Replay: real traced execution (multi-file project)
void main() {
var amounts = [12, 8, 15];
var includeShipping = false;
var total = 0;
for (var amount in amounts) {
total += amount;
}
if (includeShipping) total += 5;
print('total=$total');
}
void main() {
var amounts = [12, 8, 15];
var includeShipping = true;
var total = 0;
for (var amount in amounts) {
total += amount;
}
if (includeShipping) total += 5;
print('total=$total');
}
amounts ← [12, 8, 15]
1void main() {2 var amounts = [12, 8, 15];3 var includeShipping = false;values this step[12, 8, 15]amountsincludeShipping ← false
2var amounts = [12, 8, 15];3var includeShipping = false;4var total = 0;values this stepfalseincludeShippingtotal ← 0
3var includeShipping = false;4var total = 0;5for (var amount in amounts) {values this step0totalamount ← 12
4var total = 0;5for (var amount in amounts) {6 total += amount;values this step12amounttotal ← 12
5for (var amount in amounts) {6 total += amount;7}values this step0 → 12total12amountamount ← 8
4var total = 0;5for (var amount in amounts) {6 total += amount;values this step8amounttotal ← 20
5for (var amount in amounts) {6 total += amount;7}values this step12 → 20total8amountamount ← 15
4var total = 0;5for (var amount in amounts) {6 total += amount;values this step15amounttotal ← 35
5for (var amount in amounts) {6 total += amount;7}values this step20 → 35total15amountshipping added ← false
7}8if (includeShipping) total += 5;9print('total=$total');values this stepfalseshipping addedfalseincludeShippingprint('total=$total');
8 if (includeShipping) total += 5;9 print('total=$total');10}outputtotal=35values this step35total
amounts ← [12, 8, 15]
1void main() {2 var amounts = [12, 8, 15];3 var includeShipping = true;values this step[12, 8, 15]amountsincludeShipping ← true
2var amounts = [12, 8, 15];3var includeShipping = true;4var total = 0;values this steptrueincludeShippingtotal ← 0
3var includeShipping = true;4var total = 0;5for (var amount in amounts) {values this step0totalamount ← 12
4var total = 0;5for (var amount in amounts) {6 total += amount;values this step12amounttotal ← 12
5for (var amount in amounts) {6 total += amount;7}values this step0 → 12total12amountamount ← 8
4var total = 0;5for (var amount in amounts) {6 total += amount;values this step8amounttotal ← 20
5for (var amount in amounts) {6 total += amount;7}values this step12 → 20total8amountamount ← 15
4var total = 0;5for (var amount in amounts) {6 total += amount;values this step15amounttotal ← 35
5for (var amount in amounts) {6 total += amount;7}values this step20 → 35total15amounttotal ← 40
7}8if (includeShipping) total += 5;9print('total=$total');values this step35 → 40totaltrueincludeShippingprint('total=$total');
8 if (includeShipping) total += 5;9 print('total=$total');10}outputtotal=40values this step40total
accumulator
`total` stores the running result.
compound assignment
`total += amount` adds into the accumulator.
optional stage
The shipping branch is another small pipeline stage.