Reduction combines many values into one result by repeatedly applying a combining function.

Program

Play the script to change the starting value for the accumulation.

start
reduce_total.R
Replay: real traced execution (multi-file project)
start <- 0
values <- c(1, 2, 3)
total <- Reduce(`+`, values, init = start)
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
start <- 10
values <- c(1, 2, 3)
total <- Reduce(`+`, values, init = start)
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
start <- 20
values <- c(1, 2, 3)
total <- Reduce(`+`, values, init = start)
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
  1. start ← 0

    1start <- 02values <- c(1, 2, 3)
    values this step0start
  2. values ← 1, 2, 3

    1start <- 02values <- c(1, 2, 3)3total <- Reduce(`+`, values, init = start)
    values this step1, 2, 3values
  3. total ← 6

    2values <- c(1, 2, 3)3total <- Reduce(`+`, values, init = start)4label <- paste("total", total, sep = ":")
    values this step6total0start1, 2, 3values
  4. label ← total:6

    3total <- Reduce(`+`, values, init = start)4label <- paste("total", total, sep = ":")5cat(label, "\n", sep = "")
    values this steptotal:6label6total
  5. cat(label, " ", sep = "")

    4label <- paste("total", total, sep = ":")5cat(label, "\n", sep = "")
    outputtotal:6
    values this steptotal:6label
  1. start ← 10

    1start <- 102values <- c(1, 2, 3)
    values this step10start
  2. values ← 1, 2, 3

    1start <- 102values <- c(1, 2, 3)3total <- Reduce(`+`, values, init = start)
    values this step1, 2, 3values
  3. total ← 16

    2values <- c(1, 2, 3)3total <- Reduce(`+`, values, init = start)4label <- paste("total", total, sep = ":")
    values this step16total10start1, 2, 3values
  4. label ← total:16

    3total <- Reduce(`+`, values, init = start)4label <- paste("total", total, sep = ":")5cat(label, "\n", sep = "")
    values this steptotal:16label16total
  5. cat(label, " ", sep = "")

    4label <- paste("total", total, sep = ":")5cat(label, "\n", sep = "")
    outputtotal:16
    values this steptotal:16label
  1. start ← 20

    1start <- 202values <- c(1, 2, 3)
    values this step20start
  2. values ← 1, 2, 3

    1start <- 202values <- c(1, 2, 3)3total <- Reduce(`+`, values, init = start)
    values this step1, 2, 3values
  3. total ← 26

    2values <- c(1, 2, 3)3total <- Reduce(`+`, values, init = start)4label <- paste("total", total, sep = ":")
    values this step26total20start1, 2, 3values
  4. label ← total:26

    3total <- Reduce(`+`, values, init = start)4label <- paste("total", total, sep = ":")5cat(label, "\n", sep = "")
    values this steptotal:26label26total
  5. cat(label, " ", sep = "")

    4label <- paste("total", total, sep = ":")5cat(label, "\n", sep = "")
    outputtotal:26
    values this steptotal:26label
initial value `init` gives the reduction a starting accumulator.
combine `+` combines the accumulator with each value.
reduce `Reduce` returns one accumulated result.