function Curry----I'm sorry, I have code cleanliness.

Source: Internet
Author: User

Previously wrote a small article about a function curry

At that moment, the understanding of curry is not deep enough, usually meet the need to curry the case is relatively small, today, re-organize the problem

function curry, in fact, is a very large number of parameters of the function, in the case of most parameters are the same, the generation of a new parameter is less than the process of the function

However, there is an unresolved problem, at least I do not know what to do, is only in order to omit parameters

The code, start with a simple example.

function Add (A, B, c) {    return A + B + C;}

This method can calculate the three-digit fit.

Add (+/-);

Add (1,2,4);

Add (1,2,5);

....

In this case, always write the duplicate parameters, will not feel bored? Then look at the code below.

function /*  */Icolnum, IIndex) {    return  {        left:iindex% icolnum * (Imarginri Ght + iwidth),        Top:Math.floor (iindex/icolnum) * (Imarginbottom + iheight)    }}

If you need to implement a licensing effect now, you need to place each element in the specified position with absolute positioning.

Like this. With this method, you can get the coordinates of each block's properties and the index of the block indexed to the GetPosition method, so that it should be in the position where it should be.

If you now have a new feature, by clicking on a button, you can have them in reverse order

If you now have a new feature, you can add new blocks by clicking on one of the buttons.

If...

Well, it seems that the first five parameters in the GetPosition () method will have to be filled several times.

At this point, if you use a function curry, you can make the code look clearer,

Continue to use the Add method for instructions, and reduce the sum of three numbers to two,

And then demonstrate the process of curry the function.

The code is as follows:

function Add (A, b) {    iftypeof b = = "undefined" )        {returnfunction  (b) {            return a + b;        }    

How to use:

var add3 = Add (3) alert (ADD3 (//7

In the Add method above, a closure is returned, the first parameter A is saved in the closure, and a new parameter B is received.

This way, when we need to use a lot of add (3,??) , you can use the new function add3 () to do the calculation.

But, every time to write this, is not worth the candle, especially to the parameters really more time, is more stupid, therefore, need to encapsulate a method to achieve curry

First, we need to add a little bit of knowledge.

The Argumens in 1.function is not a real array

The 2.arrobject.slice (Start,/*optional*/end) method, which can return an element of the [Start,end] part of an array, gets the element from start to end, when only the start parameter is filled in. This method does not change the original array

3.array.prototype.slice can convert arguments into a real array.

The test code for Knowledge Point 3 is as follows

var myslice = Array.prototype.slice;
function Test (a,b,c,d) { // Myslice can convert arguments to a true array Console.dir (Myslice.call (arguments))}test (1,2,3,4)

Console display array[4];

The final function curry code is as follows

 function  Curry (fn/*  ,arg1,arg2,arg3,......  */   Array.prototype.slice;  var  Save_args = Myslice.call (arguments,1 )   function   { var  Newargs = Myslice.call (arguments);         var  args = Save_args.concat (Newargs);  return  fn.apply (null  ,args)}}  

From the principle to the middle of the use of knowledge points, has been said to be more clear, no longer repeat.

Additional test code:

<!doctype html>;} ul{List-Style:none;} li{position:absolute; width:100px; height:100px; background-color: #000;}</style><script>functionGetPosition (iwidth, Iheight, Imarginbottom, Imarginright,/*There are a few columns .*/Icolnum, IIndex) {    return{Left:iindex% Icolnum * (Imarginright +iwidth), Top:Math.floor (IIndex/icolnum) * (Imarginbottom +iheight)}}functionCurry (FN/*, Arg1,arg2,arg3,......*/){    varMyslice =Array.prototype.slice; varSave_args = Myslice.call (arguments,1)    return function (){        varNewargs =myslice.call (arguments); varargs =Save_args.concat (Newargs); returnFn.apply (NULL, args)}} Window.onload=function (){    varOul = document.getElementById ("ul"); varALis = Oul.getelementsbytagname ("li"); variwidth = 100; variheight = 100; varImargin = 10; varIcolnum = 3; varNewgetposition =Curry (getPosition, iwidth, Iheight, Imargin, Imargin, icolnum);  for(vari=0; i<alis.length; i++ ){        //var p = getPosition (iwidth, Iheight, Imargin, Imargin, Icolnum, i)        varp =newgetposition (i) Alis[i].style.top= P.top + "px"; Alis[i].style.left= P.left + "px"; }    }</script>

Come here, go to bed to zzzzzzz ....

function Curry----I'm sorry, I have code cleanliness.

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.