Created
August 31, 2012 21:28
-
-
Save jroesch/3559322 to your computer and use it in GitHub Desktop.
Javascript "times"
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
// Curried aka (Int -> String -> String) | |
var times = function (n) { | |
//closure property allows us to access n inside the inner function | |
return function (x) { | |
//code isn't executed until all parameters are passed | |
var array = []; | |
for (var i in n) { | |
array[i] = x; | |
} | |
return array.reduce(function(a, e) { return a + e; }); | |
}; | |
}; | |
//Un-curried aka ((Int -> String) -> String) | |
var untimes = function (n,x) { | |
//same body | |
} | |
var intermediate = times(5); | |
var final = intermediate("x"); | |
console.log(final); => "xxxxx" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment