Last active
December 30, 2015 20:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
power(expon) = x -> x^expon | |
square = power(2) | |
square(2) | |
cube = power(3) | |
cube(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def power(exponent): | |
return (lambda x: x**exponent) | |
square = power(2) | |
square(2) | |
cube = power(3) | |
cube(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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