From d2c1d6054518aa3ab11e82eec5ec2880c64234e0 Mon Sep 17 00:00:00 2001 From: anshulguleria Date: Sun, 31 Aug 2014 17:22:29 +0530 Subject: [PATCH] provided another solution for currying problem using recursion and Function.prototype.bind --- problems/currying/another-solution.js | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 problems/currying/another-solution.js diff --git a/problems/currying/another-solution.js b/problems/currying/another-solution.js new file mode 100644 index 0000000..2a2a885 --- /dev/null +++ b/problems/currying/another-solution.js @@ -0,0 +1,39 @@ +/* + * This program demonstrates the solution to curry problem + * using recursive functions and Function.prototype.bind + */ +function curryN(fn, n) { + var repCount = n || fn.length; + return function (arg) { + if(repCount > 1) { + //testing for 1 as for last agument we + //call the function directely. + return curryN(fn.bind(null, arg), repCount - 1); + } else { + return fn(arg); + } + }; +}; + +module.exports = curryN; + + +//test +//although function name is add +//it joins the arguments ',' seperated +/* +function add() { + return Array.prototype.join.call(arguments, ','); +}; +curry1 = curryN(add, 4); +curry2 = curry1(1); +curry3 = curry2(2); +curry4 = curry3(3); +console.error(curry4(4)); +//this part demonstrates we can change the inner function +//again to generate different set of outputs. +curry3 = curry2(6); +curry4 = curry3(3); +console.error(curry4(5)); +*/ +//uncomment the multiline comment to run the test case.