10.3 Important to remember

  1. R has First-class functions (can be created with function() and <-)

R functions are objects in their own right, a language property often called “first-class functions”
Section 6.2.3

  1. Functions capture (enclose) environment in which they are created
f <- function(x) function(y) x + y
fn_env(f)    # The function f()
#> <environment: R_GlobalEnv>
fn_env(f())  # The function created by f()
#> <environment: 0x560e54f153b8>
  1. Functions create a new environment on each run
f <- function(x) {
  function() x + 1
}
ff <- f(1)
ff()
#> [1] 2
ff()
#> [1] 2