Mapping applies the same transformation to each value and returns the transformed vector.

Program

Play the script to change the multiplier used by the mapped function.

multiplier
map_values.R
Replay: real traced execution (multi-file project)
multiplier <- 2
values <- c(2, 4, 6)
mapped <- vapply(values, function(x) x * multiplier, numeric(1))
total <- sum(mapped)
label <- paste("map", total, sep = ":")
cat(label, "\n", sep = "")
multiplier <- 1
values <- c(2, 4, 6)
mapped <- vapply(values, function(x) x * multiplier, numeric(1))
total <- sum(mapped)
label <- paste("map", total, sep = ":")
cat(label, "\n", sep = "")
multiplier <- 3
values <- c(2, 4, 6)
mapped <- vapply(values, function(x) x * multiplier, numeric(1))
total <- sum(mapped)
label <- paste("map", total, sep = ":")
cat(label, "\n", sep = "")
  1. multiplier ← 2

    1multiplier <- 22values <- c(2, 4, 6)
    values this step2multiplier
  2. values ← 2, 4, 6

    1multiplier <- 22values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))
    values this step2, 4, 6values
  3. mapped ← 4, 8, 12

    2values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)
    values this step4, 8, 12mapped2, 4, 6values2multiplier
  4. total ← 24

    3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)5label <- paste("map", total, sep = ":")
    values this step24total4, 8, 12mapped
  5. label ← map:24

    4total <- sum(mapped)5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")
    values this stepmap:24label24total
  6. cat(label, " ", sep = "")

    5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")
    outputmap:24
    values this stepmap:24label
  1. multiplier ← 1

    1multiplier <- 12values <- c(2, 4, 6)
    values this step1multiplier
  2. values ← 2, 4, 6

    1multiplier <- 12values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))
    values this step2, 4, 6values
  3. mapped ← 2, 4, 6

    2values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)
    values this step2, 4, 6mapped2, 4, 6values1multiplier
  4. total ← 12

    3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)5label <- paste("map", total, sep = ":")
    values this step12total2, 4, 6mapped
  5. label ← map:12

    4total <- sum(mapped)5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")
    values this stepmap:12label12total
  6. cat(label, " ", sep = "")

    5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")
    outputmap:12
    values this stepmap:12label
  1. multiplier ← 3

    1multiplier <- 32values <- c(2, 4, 6)
    values this step3multiplier
  2. values ← 2, 4, 6

    1multiplier <- 32values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))
    values this step2, 4, 6values
  3. mapped ← 6, 12, 18

    2values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)
    values this step6, 12, 18mapped2, 4, 6values3multiplier
  4. total ← 36

    3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)5label <- paste("map", total, sep = ":")
    values this step36total6, 12, 18mapped
  5. label ← map:36

    4total <- sum(mapped)5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")
    values this stepmap:36label36total
  6. cat(label, " ", sep = "")

    5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")
    outputmap:36
    values this stepmap:36label
function argument The anonymous function receives one value at a time.
map `vapply` applies the function to every input value.
typed result `numeric(1)` states that each mapped result is one number.