[Stupid wood Lua column] basic Supplement 05: Out-of-box iterator and out-of-box lua

Source: Internet
Author: User

[Stupid wood Lua column] basic Supplement 05: Out-of-box iterator and out-of-box lua

There is still a little bit about the content of the iterator, but it doesn't matter. It should be an extension. Let's open your eyes together ~

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

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

Source: dummies and Game Development

 

 

1. Avoid creating closed Functions

The iterator we 've been talking about before is to create closed functions, but have you ever wondered if you don't need to close the functions after you have a constant state and control variable?

Let's review the previous iterator functions:

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

This is the last dieDaiQi function introduced. At this time, it has a constant state and control variable.

Don't you think that local I variable is in the way? (Do not think)

As long as you remove it, there will be no closed function.

 

We changed the dieDaiQi function to the following:

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

We change the constant state to t, and the initial value of the control variable is still 0.

After the dieDaiQi function is called, a new function is returned, which is no longer a closed function.

So, according to the introduction in the previous article (if you don't remember it, let's look back), every time you call a function, the parameter s is the table we need, and the parameter var starts from 0, replace the local I variable.

 

How is it? I think the constant state and control variables are a little helpful?

 

2. Use the constant state to create more variables

The method just mentioned is quite good, but there is only one variable that can be changed. What if this iterator requires many variables?

In addition to using closed functions, there is also a way to turn eternity into good changes.

 

Let's continue to modify the dieDaiQi function:

 
 
  1. Function dieDaiQi (t)
  2. Return function (s, var)
  3. Var = var + 1;
  4. If var> # s. list then
  5. Return nil;
  6. End
  7. S. money = s. money * s. money;
  8. Print ("Oh, money, for me, it's just a number:". s. money );
  9. Return var, s. list [var];
  10. End, {list = t, money = 10}, 0
  11. End

Note that the returned constant state is a table ({list = t, money = 10 }).

This table remains unchanged during iteration, but the content in it is not necessarily.

Try to call this iterator:

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

The output result is as follows:

[LUA-print] Haha, money is just a number for me: 100
[LUA-print] k = 1, v = fdsd
[LUA-print] Haha, money is just a number for me: 10000
[LUA-print] k = 2, v = 445
[LUA-print] Haha, money is just a number for me: 100000000
[LUA-print] k = 3, v = 9999

 

How is it? Although each iteration is the same table, the table content changes at any time.

Just like some people, they say forever, but their actions change at any time. (Do you mean yourself ?)

 

3. The for loop iterator is not required

Now the for loop is used to call the iterator. In fact, a long time ago... There is... (Xiao RuO: Stop! I am not here to hear the story)

Well, in the past, the iterator did not use the for statement.

 

Let's simulate the previous practice:

 
 
  1. function dieDaiQiHistory(t, func)
  2.     for i = 1, #t, 1 do
  3.         func(i, t[i]);
  4.     end
  5. end

(Xiao RuO: Why didn't I do this in the first place? It's so easy. Hello !)

Ke, let's try to call this iterator:

 
 
  1.     local t = {"fdsd", "445", "9999"};
  2.     dieDaiQiHistory(t, function(k, v)
  3.         print("k=" .. k .. ", v=" .. v);
  4.     end);

Therefore, when calling the iterator, we do not need to use the for Loop (although it is still used in the iterator ).

In addition, a function must be passed as a parameter for callback to obtain the iteration value.

 

Since I don't have a lot of users with two forms of iterators, I can't compare them.

The authors in the book tend to use modern iterators.

 

I also found that some Lua library functions also use this form of iteration, which may be a historical reason, or perhaps a special application of this form.

 

4. End

Now, the iterator is over.

I feel pretty good. This is the third time I 've reviewed the previous chapter in this book, right? You still have to write and write articles to get a better understanding and be impressed.

The harder it is to stick to it later. Come on...

(Xiao RuO: That's why! Why did I use the ellipsis again! With an exclamation point, it seems more energetic ...)




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.