javascript - Currying a function that takes infinite arguments -


using es5, how curry function takes infinite arguments.

function add(a, b, c) {     return + b + c; } 

the function above takes 3 arguments want our curried version able take infinite arguments.

hence, of following test cases should pass:

var test = add(1);  test(2);     //should return 3 test(2,3);   //should return 6 test(4,5,6); //should return 16 

here solution came with:

function add(a, b, c) {     var args = array.prototype.slice.call(arguments);      return function () {         var secondargs = array.prototype.slice.call(arguments);         var totalarguments = secondargs.concat(args);          var sum = 0;          (i = 0; < totalarguments.length; i++) {             sum += totalarguments[0];         }          return sum;     } } 

however, have been told it's not “functional” in style.

part of reason add function not "functional" because attempting more add numbers passed it. confusing other developers @ code, see add function, , when call it, function returned them instead of sum.

for example:

//using add function, i'm expecting 6 add(1,2,3) //returns function = confusing! 

the functional approach

the functional approach create function allows curry other functions, , simplify add function:

function curry(fn) {     var args = array.prototype.slice.call(arguments, 1);      return function () {         return fn.apply(this, args.concat(                 array.prototype.slice.call(arguments, 0)         ));     } }  function add() {     var args = array.prototype.slice.call(arguments);      return args.reduce(function (previousvalue, currentvalue) {         return previousvalue + currentvalue;     }); } 

now, if want curry function, do:

var curry1 = curry(add, 1); console.log(         curry1(2), // logs 3         curry1(2, 3), // logs 6         curry1(4, 5, 6) // logs 16 );  //you can many arguments want var curry15 = curry(add, 1,2,3,4,5); console.log(curry15(6,7,8,9)); // logs 45 

if still want add 1, 2, 3 can do:

add(1,2,3) //returns 6, awesome! 

continuing functional approach

this code becoming reusable everywhere.

you can use curry function make other curried function references without additional hassle.

sticking math theme, lets had multiply function multiplied numbers passed it:

function multiply() {     var args = array.prototype.slice.call(arguments);      return args.reduce(function (previousvalue, currentvalue) {         return previousvalue * currentvalue;     }); }  multiply(2,4,8) // returns 64  var currymultiply2 = curry(multiply, 2); currymultiply2(4,8) // returns 64 

this functional currying approach allows take approach function, not mathematical ones. although supplied curry function not support edge cases, offers functional, simple solution problem can built upon.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -