迭代器

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
end end for 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
end end function 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 end end for 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 end end for element in StrSegIterator(instr, )
do
print(element)
end

lua自定义迭代器的更多相关文章

  1. Python 3.x自定义迭代器对象

    Python 3.x与Python 2.x之间存在着较多的语法细节差异.今天在看Python核心编程的时候,说到了自定义迭代器对象.于是动手将源码打了一遍,原书代码如下: class AnyIter( ...

  2. Python迭代和解析(4):自定义迭代器

    解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html 本文介绍如何自定义迭代器,涉及到类的运算符重载,包括__getitem_ ...

  3. Apex 中的自定义迭代器

    迭代器 迭代器(iterator)可以遍历一个集合变量中的每个元素.Apex提供了Iterator接口来让开发者实现自定义的迭代器. Iterator接口 Iterator接口定义了两个函数: has ...

  4. Lua基础---迭代器

    官方的文档说: 迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址 在Lua中迭代器是一种支持指针类型的结构,它可以遍历集合的每 ...

  5. lua 10 迭代器

    转自:http://www.runoob.com/lua/lua-iterators.html 迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表 ...

  6. python函数:叠加装饰器、迭代器、自定义迭代器、生成式

    一.叠加多个装饰器二.迭代器三.自定义迭代器四.xxx生成式 一.叠加多个装饰器 # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数 # 在加载完毕后,调用原函数其实就是在调用 ...

  7. 生成器对象(自定义迭代器),自定义range方法,模块

    自定义迭代器 一 .生成器与yield ''' 我们得到一个迭代器通常都是调用可迭代对象的__iter__方法 ,例如 list.iter() 得到一个迭代器, 但是当list很大时候,就违背了pyt ...

  8. Java 经典实例:自定义迭代器

    编写自己的Iterator,实现Iterator接口,这里多说一句,实现Iterable后,可以用"foreach"循环遍历你的对象. import java.util.Itera ...

  9. ECMAScript 2015 迭代器协议:实现自定义迭代器

    迭代器协议定义了一种标准的方式来产生一个有限或无限序列的值,并且当所有的值都已经被迭代后,就会有一个默认的返回值. 当一个对象只有满足下述条件才会被认为是一个迭代器:它实现了一个 next() 的方法 ...

随机推荐

  1. SQL SERVER中的扩展属性

    以前在SQL SERVER建表时,总看到扩展属性,但一直未使用过.今天研究下: 增加扩展属性: 语法: sp_addextendedproperty [ @name = ] { 'property_n ...

  2. IBatis.Net使用总结(二)-- IBatis返回DataTable/DataSet(网上例子的集合)

    IBatis返回DataTable,DataSet ibatis.net QueryForDataTable 完整的为ibatis.net 引入datatable支持要改动很多地方,所以描述的是最小化 ...

  3. nodejs review-01

    lesson lesson-code 05 Run your first web server 使用curl //指定方法;显示header信息 curl -X GET -i localhost:30 ...

  4. flexBox布局 -- 兼容性

    //中间留空,两侧靠边,多行显示,每行3个,一个li的宽度是30%,最后一行,如果是两个的时候会出现异常布局,可以对最后一行的最后一个li进行right:35%,android4.4以上才支持, 所以 ...

  5. Tomcat配置HTTPS方式(单向)

    简要记录主要步骤备忘 1.进入到jdk下的bin目录 2.输入如下指令 keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat ...

  6. Android入门(二):Android的系统架构

    android的系统架构和其操作系统一样,采用了分层的架构.从架构图看,android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层.   从技术方面看,An ...

  7. 我的jQuery源码读后感-jquery1.11.x 集成了 AMD

    jquery1.11.x 集成了 AMD AMD 加入到了 jQuery, jQuery 把源码切分成各个逻辑模块. ready 整合成依赖 Deferred 的模块. 有些模块被切分成一些更加利于维 ...

  8. Qt里获取目录的一个另类方法

    如果有一个文件的全路径文件名, 想获取它的路径的话, qt里我没找到比较好的办法, 都是cleanPath后, 再用QString的find, left这种函数来处理. 今天又在搞这种问题的时候, 看 ...

  9. sqlserver2008 函数1

    SQL2008 表达式:是常量.变量.列或函数等与运算符的任意组合. 1. 字符串函数 函数 名称 参数 示例 说明 ascii(字符串表达式) select ascii('abc') 返回 97 返 ...

  10. About_PHP_函数

    关于验证码的完善: //生成干扰线 $posLineX1 = rand(12,50); $posLineX2 = rand(50,110); $posX = rand(10,50); for($i=0 ...