5

I was wondering if something changed about how R handles lazy evaluation.

I am asking this after reading Hadley Wickham's AdvancedR part on the topic...

On his website (see http://adv-r.had.co.nz/Functions.html), adders and adders2 give the same result, so I guess that something changed (recently?) in how R handles lazy evaluation.

Below, a reproducible example of what I mean:

add <- function(x) {
  function(y) x + y
}
add2 <- function(x) {
  force(x)
  function(y) x + y
}
adders <- lapply(1:10, add)
adders2 <- lapply(1:10, add)
adders[[1]](10)
adders[[10]](10)
adders2[[1]](10)
adders2[[10]](10)

Historically, we would have expected to get:

> adders[[1]](10)
[1] 20
> adders[[10]](10)
[1] 20
> adders2[[1]](10)
[1] 11
> adders2[[10]](10)
[1] 20

However, on my computer (and on Hadley's website), the result is:

> adders[[1]](10)
[1] 11
> adders[[10]](10)
[1] 20
> adders2[[1]](10)
[1] 11
> adders2[[10]](10)
[1] 20

On my computer, R.Version() gives:

> R.Version()
$platform
[1] "x86_64-apple-darwin13.4.0"

$arch
[1] "x86_64"

$os
[1] "darwin13.4.0"

$system
[1] "x86_64, darwin13.4.0"

$status
[1] ""

$major
[1] "3"

$minor
[1] "2.0"

$year
[1] "2015"

$month
[1] "04"

$day
[1] "16"

$`svn rev`
[1] "68180"

$language
[1] "R"

$version.string
[1] "R version 3.2.0 (2015-04-16)"

$nickname
[1] "Full of Ingredients"

0 Answers0