迭代器

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. iframe无刷新跨域并获得返回值

    参考:http://geeksun.iteye.com/blog/1070607 /** * iframe跨域提交大数据 * @param action 跨域地址 * @param arr [ {na ...

  2. NPOI导出Excel - 自动适应中文宽度(帮助类下载)

    前言 做了好几个Excel.Word导出,用了HTTP流导出伪Excel文件.用过Office组件(这东西在生产环境下相当麻烦,各种权限,**). 最后决定使用NPOI组件来导出,好处很多很多了,这里 ...

  3. Linux Mysql 忘记用户密码

    1.停止mysql 服务 /etc/init.d/mysqld stop 2.启动mysql服务跳过授权表并在后台运行  /etc/init.d/mysqld  start  -u root --sk ...

  4. Eclipse中快速删除空行

    1.在要编辑的文档中 "Ctrl"+"F",弹出搜索框: 2.在Find文本框中输入正则表达式:^\s*\n 3.勾选正则表达式选项: 4.Find和替换所有.

  5. BZOJ2087 : [Poi2010]Sheep

    一条边能连上当且仅当它没有经过任何点,并且两边的点都是偶数个. 枚举原点,通过极角排序求出哪些边是合法的,然后区间DP即可. 时间复杂度$O(nm\log m+n^3)$. #include<c ...

  6. 西门子成立next47部门,斥资十亿欧元投资VR/AR等初创公司

       近日,西门子公司在慕尼黑举行的"西门子创新日"现场,宣布了三个关于"创新"的新动作.首先,超过六成员工的创新应用得到肯定,其中有 25 个项目获得总数高达 ...

  7. Codeforces Round #254 (Div. 2) A DZY Loves Chessboard

    先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...

  8. PHP操作字符串 截取指定长度字符 移除字符串两侧 左侧 右侧指定字符 或空白字符 替换字符

    trim() trim() 函数移除字符串两侧的空白字符或其他预定义字符. <?php $str = "Hello World!"; echo $str . "&l ...

  9. 通过Gulp使用Browsersync实现浏览器实时响应文件更改

    Gulp是什么鬼 Browsersync又是什么鬼 如何安装使用Browsersync 安装 使用 效果图 参考 Gulp是什么鬼 Gulp是一种基于node.js的构建工具,有关构建工具的概念请移步 ...

  10. 常用Oracle函数记录

    1. Oracle的replace函数与translate函数 replace函数是在字符串级别的代替,对应字符串一一替换 SQL> SELECT REPLACE('accd','cd','ef ...