[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:
- function dieDaiQi(t)
- local i = 0;
- return function(s, var)
- i = i + 1;
-
- if i > #t then
- return nil;
- end
- return i, t[i];
- end, 10, 0
- 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:
- function dieDaiQi(t)
- return function(s, var)
- var = var + 1;
- if var > #s then
- return nil;
- end
- return var, s[var];
- end, t, 0
- 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:
- Function dieDaiQi (t)
- Return function (s, var)
- Var = var + 1;
- If var> # s. list then
- Return nil;
- End
- S. money = s. money * s. money;
- Print ("Oh, money, for me, it's just a number:". s. money );
- Return var, s. list [var];
- End, {list = t, money = 10}, 0
- 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:
- local t = {"fdsd", "445", "9999"};
-
- for k, v in dieDaiQi(t) do
- print("k=" .. k .. ", v=" .. v);
- 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:
- function dieDaiQiHistory(t, func)
- for i = 1, #t, 1 do
- func(i, t[i]);
- end
- 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:
- local t = {"fdsd", "445", "9999"};
- dieDaiQiHistory(t, function(k, v)
- print("k=" .. k .. ", v=" .. v);
- 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 ...)