Reduction carries one accumulator through every input value.

Program

Play the program to sum amounts and optionally add shipping.

includeShipping
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');
}
  1. amounts ← [12, 8, 15]

    1void main() {2  var amounts = [12, 8, 15];3  var includeShipping = false;
    values this step[12, 8, 15]amounts
  2. includeShipping ← false

    2var amounts = [12, 8, 15];3var includeShipping = false;4var total = 0;
    values this stepfalseincludeShipping
  3. total ← 0

    3var includeShipping = false;4var total = 0;5for (var amount in amounts) {
    values this step0total
  4. amount ← 12

    4var total = 0;5for (var amount in amounts) {6  total += amount;
    values this step12amount
  5. total ← 12

    5for (var amount in amounts) {6  total += amount;7}
    values this step0 12total12amount
  6. amount ← 8

    4var total = 0;5for (var amount in amounts) {6  total += amount;
    values this step8amount
  7. total ← 20

    5for (var amount in amounts) {6  total += amount;7}
    values this step12 20total8amount
  8. amount ← 15

    4var total = 0;5for (var amount in amounts) {6  total += amount;
    values this step15amount
  9. total ← 35

    5for (var amount in amounts) {6  total += amount;7}
    values this step20 35total15amount
  10. shipping added ← false

    7}8if (includeShipping) total += 5;9print('total=$total');
    values this stepfalseshipping addedfalseincludeShipping
  11. print('total=$total');

    8  if (includeShipping) total += 5;9  print('total=$total');10}
    outputtotal=35
    values this step35total
  1. amounts ← [12, 8, 15]

    1void main() {2  var amounts = [12, 8, 15];3  var includeShipping = true;
    values this step[12, 8, 15]amounts
  2. includeShipping ← true

    2var amounts = [12, 8, 15];3var includeShipping = true;4var total = 0;
    values this steptrueincludeShipping
  3. total ← 0

    3var includeShipping = true;4var total = 0;5for (var amount in amounts) {
    values this step0total
  4. amount ← 12

    4var total = 0;5for (var amount in amounts) {6  total += amount;
    values this step12amount
  5. total ← 12

    5for (var amount in amounts) {6  total += amount;7}
    values this step0 12total12amount
  6. amount ← 8

    4var total = 0;5for (var amount in amounts) {6  total += amount;
    values this step8amount
  7. total ← 20

    5for (var amount in amounts) {6  total += amount;7}
    values this step12 20total8amount
  8. amount ← 15

    4var total = 0;5for (var amount in amounts) {6  total += amount;
    values this step15amount
  9. total ← 35

    5for (var amount in amounts) {6  total += amount;7}
    values this step20 35total15amount
  10. total ← 40

    7}8if (includeShipping) total += 5;9print('total=$total');
    values this step35 40totaltrueincludeShipping
  11. print('total=$total');

    8  if (includeShipping) total += 5;9  print('total=$total');10}
    outputtotal=40
    values 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.