lua自定义迭代器
迭代器
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自定义迭代器的更多相关文章
- Python 3.x自定义迭代器对象
Python 3.x与Python 2.x之间存在着较多的语法细节差异.今天在看Python核心编程的时候,说到了自定义迭代器对象.于是动手将源码打了一遍,原书代码如下: class AnyIter( ...
- Python迭代和解析(4):自定义迭代器
解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html 本文介绍如何自定义迭代器,涉及到类的运算符重载,包括__getitem_ ...
- Apex 中的自定义迭代器
迭代器 迭代器(iterator)可以遍历一个集合变量中的每个元素.Apex提供了Iterator接口来让开发者实现自定义的迭代器. Iterator接口 Iterator接口定义了两个函数: has ...
- Lua基础---迭代器
官方的文档说: 迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址 在Lua中迭代器是一种支持指针类型的结构,它可以遍历集合的每 ...
- lua 10 迭代器
转自:http://www.runoob.com/lua/lua-iterators.html 迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表 ...
- python函数:叠加装饰器、迭代器、自定义迭代器、生成式
一.叠加多个装饰器二.迭代器三.自定义迭代器四.xxx生成式 一.叠加多个装饰器 # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数 # 在加载完毕后,调用原函数其实就是在调用 ...
- 生成器对象(自定义迭代器),自定义range方法,模块
自定义迭代器 一 .生成器与yield ''' 我们得到一个迭代器通常都是调用可迭代对象的__iter__方法 ,例如 list.iter() 得到一个迭代器, 但是当list很大时候,就违背了pyt ...
- Java 经典实例:自定义迭代器
编写自己的Iterator,实现Iterator接口,这里多说一句,实现Iterable后,可以用"foreach"循环遍历你的对象. import java.util.Itera ...
- ECMAScript 2015 迭代器协议:实现自定义迭代器
迭代器协议定义了一种标准的方式来产生一个有限或无限序列的值,并且当所有的值都已经被迭代后,就会有一个默认的返回值. 当一个对象只有满足下述条件才会被认为是一个迭代器:它实现了一个 next() 的方法 ...
随机推荐
- MVC 教程汇总
http://www.cnblogs.com/powertoolsteam/archive/2015/08/13/4667892.html
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...
- CS: Marshalling and Unmarshalling, Serialization and Unserialization
Link1: https://en.wikipedia.org/wiki/Marshalling_(computer_science) Quote: " Comparison with se ...
- spring异常-aoperror at :0 formal unbound in pointcut
八月 17, 2016 10:15:21 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRe ...
- 【Linux】df命令 ,查看磁盘容量。
Oracle 导库时,失败,原因为磁盘满了, 记录下查看磁盘容量的指令 1.命令格式: df [选项] [文件] -a 全部文件系统列表 -h 方便阅读方式显示 -H 等于“-h”,但是计算式,1K= ...
- C++STL 常用 函数 用法
学完c++快一年了,感觉很有遗憾,因为一直没有感觉到c++的强大之处,当时最大的感觉就是这个东西的输入输出比C语言要简单好写. 后来我发现了qt,opencv,opengl,原来,c++好玩的狠. 在 ...
- javascript学习之运动框架
模仿新浪博客首页的,最新评论: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"&g ...
- 最难面试的IT公司之ThoughtWorks代码挑战——FizzBuzzWhizz游戏
最近互联网招聘平台拉勾网在五一期间推出个“最难面试的IT公司”代码挑战活动,评选出了5个最难面试的IT公司,即:ThoughtWorks.Google.Unisys.Rackspace.Cypress ...
- Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector
Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同元素.在列表上迭代通常优于索引遍历.特殊的ListIterator迭代器允许元素插入.替换,双向访问, ...
- 【5集iCore3_ADP演示视频】5-1 iCore3应用开发平台开箱视频
iCore3双核心应用开发平台基于iCore3双核心板,包含ARM.FPGA.7寸液晶屏.双通道数字示波器.任意波发生器.电压表等模块,是一款专为电子爱好者设计的综合性电子学习系统. [视频简介]本视 ...