[Binwood Lua column] basic Supplement 04: for Loop and iterator secrets, lua Column

Source: Internet
Author: User

[Binwood Lua column] basic Supplement 04: for Loop and iterator secrets, lua Column

As we introduced in the previous article, the for loop can be used to call the iterator, which is very simple.

So what does this for loop do? Of course I didn't go to the source code. I just read a book.

The materials are from the second edition of Lua program design. If there is no error in the content of this book, there will be no errors in this article theoretically ~

Stupid wood and flowers contribute, huh? Flowers? No, it's your heart ~

Reprinted please note, original address: http://www.benmutou.com/archives/1717

Source: dummies and Game Development

1. iterator that returns two values

Pairs can traverse the key and value of the table, while the dieDaiQi function we previously wrote can only return value.

Therefore, we need to change the dieDaiQi function as follows:

 
 
  1. function dieDaiQi(t)
  2.     local i = 0;
  3.     return function()
  4.         i = i + 1;  
  5.        
  6.         if i > #t then
  7.             return nil;
  8.         end          
  9.         return i, t[i];
  10.     end
  11. end

Of course, this is not a secure iterator. We suppose there is no nil value in the table.

As to why there is an if I> # t judgment, we will discuss it later.

 

Call the iterator as follows:

 
 
  1.     local t = {"fdsd", "445", "9999"};
  2.     for k, v in dieDaiQi(t) do
  3.         print(k .. "," .. v);
  4.     end

The output result is as follows:

[LUA-print] 1, fdsd
[LUA-print] 2,445
[LUA-print] 3,9999

 

2. for... in... do

[For k, v in dieDaiQi (t) do end] This code is actually equivalent to the following code:

 
 
  1.     do
  2.         local _f, _s, _var = dieDaiQi(t);
  3.        
  4.         while true do
  5.             local k, v = _f(_s, _var);
  6.             _var = k;
  7.            
  8.             if _var == nil then
  9.                 break;
  10.             end
  11.            
  12.             print(k .. "," .. v);
  13.         end
  14.     end

Is it complicated? In fact, it is very similar to the code we used to call the iterator for the first time. we delete the complicated part first and the code becomes as follows:

 
 
  1.     do
  2.         local _f = dieDaiQi(t);
  3.        
  4.         while true do
  5.             local k, v = _f();
  6.            
  7.             if k == nil then
  8.                 break;
  9.             end
  10.            
  11.             print(k .. "," .. v);
  12.         end
  13.     end

Run this Code. The result is as follows:

[LUA-print] 1, fdsd
[LUA-print] 2,445
[LUA-print] 3,9999

 

This is the same as using the for in loop directly.

 

In fact, all I'm talking about is nonsense, because we have already said that the for in loop is used to simplify the call of the iterator, so of course it is the same result.

 

3. iterator functions, constant state, initial control variable values

Let's take a look at the first line of code for in: local _ f, _ s, _ var = dieDaiQi (t );

The three return values represent the iterator function (_ f), constant state (_ s), and initial value of the control variable (_ var ).

 

Iterator function: You don't need to explain it. It is the closed function returned by dieDaiQi.

Constant state: it is actually a variable. This variable remains unchanged, so it is called constant.

Initial Value of the control variable: relative to a constant value, which is a variable that changes constantly.

 

Because I have never actually used this feature, I cannot give practical examples and can only explain it theoretically.

1. For example, our dieDaiQi function has only one return value, which is the closed function. Therefore, both _ s and _ var are nil.

2. Then, call local k, v = _ f (_ s, _ var). In fact, this is to call the closed function and pass the constant value and variable value as parameters.

3. lua functions are very free. Even if the _ f function itself does not have any parameters, you can pass the parameters in without affecting anything. Therefore, two nil values are passed in, and nothing happens, just like calling _ f () directly.

4. the following code: _ var = k; this stores the first return value of the closed function (_ f), because the return value of every call to the closed function (_ f) is the next iteration value, SO _ var is a different value each time.

5. If the value of _ var is nil, stop the loop and end the iteration.

 

Therefore, when writing an iterator, the iteration end is to make the first return value nil.

 

So what is the case if we let the dieDaiQi function return a constant state and an initial value of the control variable?

The Code is as follows:

 
 
  1. Function dieDaiQi (t)
  2. Local I = 0;
  3. Return function (s, var)
  4. I = I + 1;
  5. If I> # t then
  6. Return nil;
  7. End
  8. Print ("constant value ="... s... ", variable value ="... var)
  9. Return I, t [I];
  10. End, 10, 0
  11. End

Note that the dieDaiQi function now returns three parameters, followed by 10 and 0, respectively, the constant state and the initial value of the control variable.

At the same time, the closed function also has two parameters: s and var.

 

So we use the for loop iterator again:

 
 
  1.     for k, v in dieDaiQi(t) do
  2.         print(k .. "," .. v);
  3.     end

The output result is as follows:

[LUA-print] constant value = 10, variable value = 0
[LUA-print] 1, fdsd
[LUA-print] constant value = 10, variable value = 1
[LUA-print] 2,445
[LUA-print] constant value = 10, variable value = 2
[LUA-print] 3,9999

 

The constant value is always the same, and the variable value is assigned a value of k after each closed function is called. Therefore, the variable value is always changed according to the table key value.

It may be a bit confusing at the moment. However, as long as you compare the implementation code corresponding to for... in... do... end, it is easy to understand.

 

4. End

I have finally finished writing. I can't hold it anymore. It's enough to write two articles one night.

My eyes are full of flowers... I don't know how many nights I can stick...

Fortunately, the learning content is getting harder and harder, so that I can't understand it all night, and I can't write a tutorial every night ~

That's great. (Xiao RuO: If you want to be lazy, just be lazy and say so much)

 


Java, iterator and for Loop Problems

Because method 2 is incorrect! Once you enter
If (! W. isLive)
{
This. fio. getH_gun (). remove (w );
}
This code will remove an element, and the subsequent elements will move forward. The subscript of all subsequent elements will be reduced by 1, so that the next element cannot be traversed
You should
If (! W. isLive)
{
This. fio. getH_gun (). remove (w );
I --;
}
Or Loop
For (int I = this. fio. getH_gun (). size ()-1; I> = 0; I --)
This is also true for the forward loop.

Exceptions will certainly recur. Find the specific cause for yourself!

If there is an iterator in the for loop, int I = 0 cannot be defined.

This is not an iterator problem.

Two variables cannot be defined in the same statement.
You cannot include commas in any case.
This situation is defined externally.

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.