JS - sloppy mode curry

In this example below you will see how to do a JS - sloppy mode curry with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by ingosr, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright ingosr ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>JS - sloppy mode curry</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/ingosr/js-sloppy-mode-curry-YrLbGd */
/**
* by ariel flesler 2008
* @note: uses the old arguments.callee not supported in "strict" js
* @usage examples: http://demos.flesler.com/Curry/
* @note: the examples includes all functional things map, reduce, etc... 
* @orginal: http://demos.flesler.com/Curry/js/Curry.js
*/
function curry( fn ){
	// return a function
	return function(){ // <-- master
		var 
  	// run curry's custom "args" function with the arguments passed to the returned function
    // to convert the arguments passed to the returned function into an array
    args = curry.args(arguments),
    
    // memorize the returned function
		master = arguments.callee,
    
    // store the current object the returned function is called for
		self = this;
    
		// if arguments were passed to the returned function
		return args.length >= fn.length ? 
    
    // recall the fn function and pass it the args ARRAY
    fn.apply(self,args) :	
    
    // if no arumgents were passed, return a function
    function(){
    	// that will, if called, run again the anonymous returned function (master)
    	// with an ARRAY of arguments create from the original and any new received arguments.  
			return master.apply( self, args.concat(curry.args(arguments)) );
		};
	};
};

curry.args = function( args ){
	return Array.prototype.slice.call(args);
};

Function.prototype.curry = function(){
	return curry(this);
};





// usage example
// Math  
var sum = curry(function( x, y ){ 
  console.log("sum")
    return x + y;  
});  
var inc = sum(1);  
var dec = sum(-1);  

var r = sum(1)(9);
console.log(r);

Comments