Skip to content

Instantly share code, notes, and snippets.

@jroesch
Created August 31, 2012 21:28
Show Gist options
  • Save jroesch/3559322 to your computer and use it in GitHub Desktop.
Save jroesch/3559322 to your computer and use it in GitHub Desktop.
Javascript "times"
// 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