R's lazy evaluation can lead to different results when translating map function calls to for loops. Below is one example that Claus Wilke provided on Twitter.
Lets think about, whether there is someway to force evaluation programmatically to produce the same result as map and if not, this needs to be mentioned an a vignette together with other caveats and quirks of as_loop().
library(loopurrr)
#> Loading required package: purrr
make_add <- function(n) {
function(x) {
x + n
}
}
idx <- as.list(1:5)
z <- map(idx, make_add)
map(idx, make_add) %>%
as_loop()
# --- convert: `map(idx, make_add)` as loop --- #
out <- vector("list", length = length(idx))
for (i in seq_along(idx)) {
out[[i]] <- make_add(idx[[i]])
}
# --- end loop --- #
z[[1]](10)
#> [1] 11
out[[1]](10)
#> [1] 15
Created on 2022-04-05 by the reprex package (v0.3.0)
R's lazy evaluation can lead to different results when translating
mapfunction calls toforloops. Below is one example that Claus Wilke provided on Twitter.Lets think about, whether there is someway to force evaluation programmatically to produce the same result as
mapand if not, this needs to be mentioned an a vignette together with other caveats and quirks ofas_loop().Created on 2022-04-05 by the reprex package (v0.3.0)