This is an example from Hadley's latest book (Advanced R). I hope he does not mind that I posted it.
I am having difficulty to understand why this code snippet does what it does. Could someone expand on this? How can I divide it into mini steps?
add <- function(x) {
function(y) x+y
}
adders <- lapply(1:10,add)
I understand a simpler call to "add" function.
> add(2)(1)
[1] 3
In a way, 2 is assigned to x and 1 is assigned to y.
When we use this function with lapply (this way), how does y get assigned a value?