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
multiplier <-
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 <-
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 <-
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 = "")
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.