x <- sample(LETTERS, 10)
x
large_x <- sample(rep(x, 10))
length(large_x)
slow_func <- function(x) {
for (i in x) {
Sys.sleep(0.001)
}
tolower(x)
}
system.time({
y1 <- slow_func(large_x)
})
system.time({
y2 <- with_deduped(slow_func(large_x))
# Can also use the R pipe (R >= 4.1.0) or magrittr pipe, for convenience.
# slow_func(large_x) |> with_deduped()
})
all(y1 == y2)
Run the code above in your browser using DataLab