首页 技术 正文
技术 2022年11月19日
0 收藏 615 点赞 2,828 浏览 2590 个字

迭代器

http://www.tutorialspoint.com/lua/lua_iterators.htm

迭代器能够让你遍历某个集合或者容器中的每一个元素。 对于lua来说, 集合通常指代 table, 用于创建变化的数据结构, 类似数组。

Iterator is a construct that enables you to traverse through the elements of the so called collection or container. In Lua, these collections often refer to tables, which are used to create various data structures like array.

通用For迭代器

通常使用的for循环, 配合in使用, in后的参数 就是一个迭代器函数。

A generic for iterator provides the key value pairs of each element in the collection. A simple example is given below.

array = {"Lua", "Tutorial"}for key,value in ipairs(array)
do
print(key, value)
end

对于迭代器函数, 根据迭代器函数中状态的维护, 可以分为如下两种类型, 有状态迭代器, 和 无状态迭代器。

In Lua we use functions to represent iterators. Based on the state maintenance in these iterator functions, we have two main types −

  • Stateless Iterators
  • Stateful Iterators

无状态迭代器

迭代器函数中不维护任何状态。

By the name itself we can understand that this type of iterator function does not retain any state.

Let us now see an example of creating our own iterator using a simple function that prints the squares of n numbers.

如下例子, 迭代状态, 由for的参数 i 记录。

function square(iteratorMaxCount,currentNumber)   if currentNumber<iteratorMaxCount
then
currentNumber = currentNumber+1
return currentNumber, currentNumber*currentNumber
endendfor i,n in square,3,0
do
print(i,n)
end

改进版:

function square(iteratorMaxCount,currentNumber)   if currentNumber<iteratorMaxCount
then
currentNumber = currentNumber+1
return currentNumber, currentNumber*currentNumber
endendfunction squares(iteratorMaxCount)
return square,iteratorMaxCount,0
end for i,n in squares(3)
do
print(i,n)
end

有状态迭代器

利用闭包,将状态管理在闭包类, 迭代器函数为闭包。

The previous example of iteration using function does not retain the state. Each time the function is called, it returns the next element of the collection based on a second variable sent to the function. To hold the state of the current element, closures are used. Closure retain variables values across functions calls. To create a new closure, we create two functions including the closure itself and a factory, the function that creates the closure.

Let us now see an example of creating our own iterator in which we will be using closures.

如下例子,推荐使用如下写法,减少信息暴漏:

array = {"Lua", "Tutorial"}function elementIterator (collection)   local index = 0
local count = #collection -- The closure function is returned return function ()
index = index + 1 if index <= count
then
-- return the current element of the iterator
return collection[index]
end endendfor element in elementIterator(array)
do
print(element)
end

自定义例子

使用有状态迭代器, 实现字符串拆分为固定长度的字符串:

local instr = "2334t545dfgjkkkk"function StrSegIterator (str, segSize)
local strIndex = -- The closure function is returned
return function ()
local segStart = strIndex
local segEnd = strIndex + segSize -
local strseg = string.sub(str, segStart, segEnd) if #strseg > then
strIndex = strIndex + segSize -- return the current element of the iterator
return strseg
end endendfor element in StrSegIterator(instr, )
do
print(element)
end
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,428
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726