迭代器与泛型for
迭代器与closure
function allwords()
local line=io.read()
local pos=
return function()
while line do
local s,e=string.find(line,"%w+",pos)
if s then
pos=e+
return string.sub(line,s,e)
else
line=io.read()
pos=
end
end
print("function endddddddddd")
return nil
end
end for word in allwords() do
print()
print(word)
end
无状态的迭代器
local function iter(a,i)
i=i+
local v=a[i]
if v then
return i,v
end
end
function ipairs(a)
-- return iter,a,0 --正确--for获得3个值,迭代器函数,恒定状态,以及控制变量
return iter(a,) --错误
end a={,,,}
--for k,v in ipairs(a) do
-- print(k,v)
--end for k,v in next,a do
print(k,v)
end
复杂迭代器
--复杂迭代器,不需要定义局部变量pos
local iterator
function allwords()
local state={line=io.read(),pos=}
return iterator,state
end function iterator(state)
while state.line do
local s,e=string.find(state.line,"%w+",state.pos)
if s then
state.pos=e+
return string.sub(state.line,s,e)
else
state.line=io.read()
state.pos=
end
end
return nil
end for word in allwords() do
print()
print(word)
end
真实迭代器
function allwords(f)
for line in io.lines() do
for word in string.gmatch(line,"%w+") do
f(word)
end
end
end
allwords(print)
迭代器与泛型for的更多相关文章
- 迭代器、泛型和增强For
Iterator hasNext next Iterator 迭代器 Collection提供了一个遍历集合的通用方式,迭代器(Iterator). 获取迭代器的方式是使用Collection定义的 ...
- Lua学习(5)——迭代器与泛型for
1. 迭代器 2. 泛型for语义 所谓迭代器就是一种可以遍历一种集合中所有元素的机制.在lua中,迭代器通常表示为函数,每调用依次函数就返回集合中的下一个元素.泛型for 内部保存了迭代器函数 实际 ...
- Step By Step(Lua迭代器和泛型for)
Step By Step(Lua迭代器和泛型for) 1. 迭代器与Closure: 在Lua中,迭代器通常为函数,每调用一次函数,即返回集合中的"下一个"元素.每个迭代器都 ...
- Lua迭代器和泛型for
1.迭代器与closure 在lua中,迭代器通常为函数,每调用一次函数,会返回集合中的下一个元素.每个迭代器在成功调用的时候,都需要保存一些状态,closure(闭包)完美为迭代器运用而生. fun ...
- Java基础学习笔记十五 集合、迭代器、泛型
Collection 集合,集合是java中提供的一种容器,可以用来存储多个数据. 在前面的学习中,我们知道数据多了,可以使用数组存放或者使用ArrayList集合进行存放数据.那么,集合和数组既然都 ...
- Lua中的迭代器与泛型for
[前言] 迭代器就是一种可以遍历一种集合中所有元素的机制,在Lua中,通常将迭代器表示为函数.每调用一次函数,就返回集合中的“下一个”元素.每个迭代器都需要在每次成功调用之后保存一些状态,这样才能知道 ...
- lua迭代器和泛型for浅析
(一) 首要概念要理清: 1. 在lua中,函数是一种"第一类值",他们具有特定的词法域."第一类值"表示在lua中函数与其他传统类型的值(例如数字和字符串)具 ...
- 《Lua程序设计》第7章 迭代器与泛型for 学习笔记
本章将介绍如何编写适用于泛型for的迭代其(Iterator).7.1 迭代器与closurehttp://www.cnblogs.com/moonlightpoet/p/5685275.html 7 ...
- Java 集合、Iterator迭代器、泛型等
01集合使用的回顾 A:集合使用的回顾 a.ArrayList集合存储5个int类型元素 public static void main(String[] args) { ArrayList<I ...
- JAVA基础之集合、Iterator迭代器、泛型及增强for循环
个人理解: 对于集合,首先要明确的是最顶层的接口是Collection接口类,其包含一些基本的方法以便子类调用,不过在定义的时候最好定义好数据类型,以免遍历时还得必须进行向上转型:特别注意的是其没有关 ...
随机推荐
- Ubuntu14.04下 安装p4c
参考: Github p4c README Ubuntu14.04下 安装p4c 这里提供一个直接安装p4c的脚本:install_p4c.sh. 1.git clone下来p4c: $ git cl ...
- 用flvplayer.swf在网页中播放视频(网页中flash视频播放的实现)
原:http://blog.csdn.net/ricciozhang/article/details/46868201 由于公司项目的需求,需要在展示一些信息的时候能够播放视频,拿到这个要求,我就从最 ...
- sql存储过程基本语法
一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 declare @user1 nvarchar() select @user1='张三' pr ...
- [原][粒子特效][spark]粒子系统system、主节点group、渲染器render
深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html system: A class defining a complete sy ...
- Rxbus的使用
Rxbus是一种模式,在RxJava中 一.添加依赖 compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxjava:1.1.5 ...
- Java代码优化,都有哪些常用方法?
Java代码优化是Java编程开发很重要的一个步骤,Java代码优化要注重细节优化,一个两个的细节的优化,产生的效果不大,但是如果处处都能注意代码优化,对代码减少体积.提高代码运行效率是有巨大帮助的, ...
- leecode第十五题(三数之和)
class Solution { public: void quick_order(vector<int>& num, int star, int en)//快排 { int st ...
- python的try finally (还真不简单)
https://www.cnblogs.com/cotton/p/3785999.html def f(): try: print 1 return 1 finally: print 0 return ...
- HTML 标记 1
1. 文件结构 <html> ----------------------开始 <head> -------------- ...
- new int
new int, 在申请内存,定义int变量:new int (100),在申请内存,定义int变量,并初始化为100:new int[100] , 在申请内存,定义int数组变量.