Skip to content

Instantly share code, notes, and snippets.

@multidis
Last active December 30, 2015 20:29
Show Gist options
  • Save multidis/7880944 to your computer and use it in GitHub Desktop.
Save multidis/7880944 to your computer and use it in GitHub Desktop.
Closure simple example in selected technical computing languages. NOTE: performance checks are recommended, particularly in Julia and Python. In R on the other hand functional programming is nearly-native, and does not seem to slow down already slow code.
power(expon) = x -> x^expon
square = power(2)
square(2)
cube = power(3)
cube(2)
def power(exponent):
return (lambda x: x**exponent)
square = power(2)
square(2)
cube = power(3)
cube(2)
power <- function(exponent) {
return (function(x) x^exponent)
}
square <- power(2)
square(2)
cube <- power(3)
cube(2)
## NOTE also useful double arrow assignment <<- in R
## http://stackoverflow.com/questions/2628621/how-do-you-use-scoping-assignment-in-r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment