Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created January 17, 2013 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreyiacono/4554297 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/4554297 to your computer and use it in GitHub Desktop.
closures
> make.power <- function(n) {
pow <- function(x) {
x ^ n
}
pow
}
> cube <- make.power(3)
> square <- make.power(2)
> cube(3)
[1] 27
> square(3)
[1] 9
> ls(environment(cube))
[1] "n" "pow"
> get("n", environment(cube))
[1] 3
> get("n", environment(square))
[2] 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment