Created
April 9, 2018 14:56
-
-
Save DanyelMorales/18a8b5044073b88ab3a81dacf0363892 to your computer and use it in GitHub Desktop.
Curry stable method for javascript and Typescript.
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
// Define new methods, function helper | |
Function.method = function(name, fx) { | |
this.prototype[name] = fx; | |
}; | |
// Define curry method | |
Function.method("curry", function() { | |
const slice = Array.prototype.slice; | |
const args = slice.apply(arguments); | |
const that = this; | |
return function() { | |
return that.apply(null, args.concat(slice.apply(arguments))); | |
}; | |
}); | |
// Original function | |
function add(a, b) { | |
return a + b; | |
} | |
// creating our curried function, and invoking | |
const add1 = add.curry(1); | |
// returns 46 | |
console.log(add1(45)); | |
// invoking our function with no curry | |
// returns 3 | |
console.log(add(1, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment