Iterations and iterators

Source: Internet
Author: User

Iterator Deduction 1. General functions
var func = function () {console.log ("Func executed"); return 123;}    Console.log (func ());  function return value 123;console.log (func); The address of the Func is returned
2. Function of parameter transfer
var func = function (a) {Console.log ("Func executed, received parameter:", a); return 123;}   Console.log (func (1)); function return value 123;
3. Try to have the function loop execution with a for
var func = function (a) {Console.log ("Func executed, received parameter:", a); return 123;}    for (i=1;10;1) {var v = func (i);   Console.log (v);} Move the execution statement inside the Loop statement
4. Change the FOR loop: iteration
var func = function (a) {Console.log ("Func executed, received parameter:", a); return 123;}       V is the Func function return value, can have more than one return value, but will only return the first for (V in func) {//func () () to delete, or error console.log (v);} The loop execution will not stop here!
5. Specify the first execution parameter
var func = function (a) {Console.log ("owner:", owner) Console.log ("Func executed, received parameter:", a);    Console.more (1); return 123;} The 1:owner parameter, 2 is the first call parameter for (v in func,1,2) {//onwer parameter is generally not used can be left blank omitted Console.log (v);} And it's going to be a constant execution.

Run it and see that the first time you receive the parameter is 2, the second start parameter is the previous Func function return value 123.

6. Let the iterator return null to end the loop
var func = function (a) {Console.log ("Func executed, received parameter:", a);    a++;     if (a<=10) {return A;    } else{return null; }}for (v in func,,1) {//here () to delete, otherwise error console.log (v);}
7. Use a function to represent func,,1
var iter = function () {return func,,1;}    For (v in ITER ()) {//here need (), carefully experience!!! Console.log (v);}
8. The function name is also a variable, put the Func inside the ITER:
var iter = function () {var func = function (a) {Console.log ("Func executed, received parameter:", a);        a++;         if (a<=10) {return A;        } else{return null; }} return func,,1;} For (v in ITER ()) {Console.log (v);}
9. ITER is a function that can receive variables, which change the number of iterators for loops to variables
var iter = function (len) {var func = function (a) {Console.log ("Func executed, received parameter:", a);        a++;         if (A<=len) {return A;        } else{return null; }} return func,,1;} For (v in ITER) {Console.log (v);}
10. Re-optimization, the cycle of the initial value is also changed to a variable
Var iter = function (I,len) {    var func = function (a) {  A is the control variable of the iterator         console.log ("Func executes, receives the parameter:", a);         a++;        if (A<=len) {             return a;          }        else{             return null;        }     }       return func,,i;} Create iterator function (builder) for (V in iter (2,15)) {            console.log (v);}

Summary: iterators separate business and logic code, implement code modularity, and reduce code coupling.

A function, called a function, returns the function implementation within the function. Return of the function is the return value, is the function after the execution of the value, the direct output function, is the memory address of the function object itself. When using variables, use local variables as much as possible, because local variables are scoped, so the speed is faster.


Iteration:

function, the last time you execute the function's return value as input. That is, the result of the last achievement is always taken as the next parameter to execute, or the cost of the next execution. This is the work of iterating over the values and approaching the target continuously. The function used to do the iteration, called an iterator.

The function in the iterator, put the function itself, the result of the last execution, is the next parameter, then the first time the execution, the parameters are? If you want to pass parameters to this function, you need to separate the functions with commas, and then a comma for each parameter. However, the first argument, the owner of the inner function, represents which function the outer iterator is calling. Therefore, the first parameter is the owner parameter, which can generally be not written. "func,,2,3"

When a null is return in Func during an iteration, the iteration is stopped. Control Iteration Parameters

Iterators can pass Exchange code control between functions, separating code logic and facilitating better modular code.

The function name of the generator of the iterator is typically started with ' each ', for example Com.each ()/table.eachname/process.each ()/string.gmatch ()



This article from the "One Night Off" blog, declined to reprint!

Iterations and iterators

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.