Functional Programming Patterns
Map Values
Apply One Function
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.
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 = "")
multiplier ← 2
1multiplier <- 22values <- c(2, 4, 6)values this step2multipliervalues ← 2, 4, 6
1multiplier <- 22values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))values this step2, 4, 6valuesmapped ← 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, 6values2multipliertotal ← 24
3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)5label <- paste("map", total, sep = ":")values this step24total4, 8, 12mappedlabel ← map:24
4total <- sum(mapped)5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")values this stepmap:24label24totalcat(label, " ", sep = "")
5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")outputmap:24values this stepmap:24label
multiplier ← 1
1multiplier <- 12values <- c(2, 4, 6)values this step1multipliervalues ← 2, 4, 6
1multiplier <- 12values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))values this step2, 4, 6valuesmapped ← 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, 6values1multipliertotal ← 12
3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)5label <- paste("map", total, sep = ":")values this step12total2, 4, 6mappedlabel ← map:12
4total <- sum(mapped)5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")values this stepmap:12label12totalcat(label, " ", sep = "")
5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")outputmap:12values this stepmap:12label
multiplier ← 3
1multiplier <- 32values <- c(2, 4, 6)values this step3multipliervalues ← 2, 4, 6
1multiplier <- 32values <- c(2, 4, 6)3mapped <- vapply(values, function(x) x * multiplier, numeric(1))values this step2, 4, 6valuesmapped ← 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, 6values3multipliertotal ← 36
3mapped <- vapply(values, function(x) x * multiplier, numeric(1))4total <- sum(mapped)5label <- paste("map", total, sep = ":")values this step36total6, 12, 18mappedlabel ← map:36
4total <- sum(mapped)5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")values this stepmap:36label36totalcat(label, " ", sep = "")
5label <- paste("map", total, sep = ":")6cat(label, "\n", sep = "")outputmap:36values 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.