Javascript-the Good Parts function Curry

Source: Internet
Author: User

Functions is values, and we can manipulate function values in interesting ways. Currying allows us to produce a new function by combining a function and an argument:

var add1 = Add.curry (1);d Ocument.writeln (Add1 (6));    // 7

Add1 is a function this was created by passing 1 to add ' s Curry method. The ADD1 function adds 1 to its argument. JavaScript does not has a curry method, but we can fix this by augmenting Function.prototype:

 function.method (' Curry ', function   () {
    var  args = arguments, that = this  Span style= "color: #000000;"    >;  return  function   () { return  that.apply (null  
   
    , Args.concat (arguments));    };});  
    // 
     Something isn ' t right ...  
   

The Curry method works by creating a closure, that holds, original function and the arguments to curry. It returns a function, when invoked, returns the result of calling that original function, passing it all of the Argu Ments from the invocation of curry and the current invocation. It uses the Array concat method to concatenate the arrays of arguments together.

Unfortunately, as we saw earlier, the arguments array is not a array, so it does not has the Concat method. To work around, we'll apply the array slice method on both of the arguments arrays. This produces arrays, behave correctly with the Concat method:

function () {    var slice = Array.prototype.slice,        = slice.apply (arguments),          This ;     return function () {        return that.apply (null, Args.concat (slice.apply (arguments));    };}) ;

I want to add the Curry method to all the function objects:

function Add () {    console.log (arguments);} Add.curry ();

Operation Result:

>Add.curry (); Typeerror:objectfunctionAdd () {Console.log (' Add ');} Has no method' Curry 'At REPL:1:5At REPLServer.self.eval (repl.js:110:21) at Repl.js:249:20At REPLServer.self.eval (repl.js:122:7) at Interface.<anonymous> (repl.js:239:12) at Interface.emit (events.js:95:17) at Interface._online (readline.js:202:10) at Interface._line (readline.js:531:8) at Interface._ttywrite (readline.js:760:14) at Readstream.onkeypress (readline.js:99:10)

In fact, add is a function object, all functions are object of function, it inherits the Function.prototype:

function () {    var slice = Array.prototype.slice,        = slice.apply (arguments),          This ;     return function () {        return that.apply (null, Args.concat (slice.apply (arguments));    };} 

Now all the method objects will have the Curry method:

> ADD2 = Add.curry (1) [Function]> add2 (2,3' 0 ': 1, ' 1 ': 2, ' 2 ': 3 }undefined> add2 (2,3, 4' 0 ': 1, ' 1 ': 2, ' 2 ': 3, ' 3 ': 4 }undefined

Javascript-the Good Parts function Curry

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.