迭代器

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. 已解决:ECSHOP安装出现date_default_timezone_get()问题

    今天在安装ECSHOP时遇到警告如下: Warning: date_default_timezone_get(): It is not safe to rely on the system's tim ...

  2. map 和 vector 的erase函数说明

    1. map的erase函数使用 这里首先要注意,C++针对map的erase函数有不同的函数原型,这往往是出现问题的关键所在.根据参考文献1: 在C++98中: (1) void erase (it ...

  3. html上下结构(上部固定高度,下部平铺)

    html页面上下结构: 上部固定,下部平铺 <div id="page_header"></div> <div id="page_conte ...

  4. 解决Win8无法升级.NET Framework 3.5.1 提示错误0x800F0906

    搞了好久,发现了这篇文,很清晰,就引用了过来.http://www.xdowns.com/article/239/Article_3065.html 起因是windows8.1装oracle10g提示 ...

  5. 团队作业week9 scenario testing

    1.How do you expect different personas to use your software? What’s their need and their goals,  how ...

  6. cxf webservice简单应用

    Server端 server部署到一个端口号为80的tomcat中 CXFController.java package com.lwj.controller; import java.io.IOEx ...

  7. 兼容IE浏览器的js浏览器全屏代码

    众所周知,IE是个奇葩的浏览器,但是由于用户量很大,开发者还是不得不为IE考虑一下,于是,各种浏览器相关的操作,都要多一个特别的判断——专门针对IE浏览器的判断,这里的全屏也不例外.看代码: func ...

  8. Unity学习疑问记录之触摸点坐标

    Vector3 pos=Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); 类似的鼠标点击Camera.main.ScreenToW ...

  9. 2.线性表-Linked list

    fatal.h #include <stdio.h> #include <stdlib.h> #define Error( Str ) FatalError( Str ) #d ...

  10. js中列表控件排序箭头,在wke中不支持的解决办法

    列表中箭头,实际使用的在线css样式,wke不支持排序箭头(实际是字体)的在线css样式,可以客户端安装对应的字体,之后显示就正常了.