lua coroutine for iterator
背景
前面的文章演示了使用闭包函数实现 状态的迭代器。
本文演示使用 coroutine来产生迭代器的例子。
coroutine迭代器例子 -- 遍历二叉树
- local binary_tree = {
- data = ,
- left = {
- data = ,
- left = { data = },
- right = { data = }
- },
- right = {
- data = ,
- left = {
- data = ,
- left = { data = 5.5 },
- right = { data = 7.4}
- },
- right = { data = }
- }
- }
- function tree_iterator(root)
- local function visit_inorder(node)
- if node.left ~= nil then
- visit_inorder(node.left)
- end
- coroutine.yield(node.data)
- if node.right ~= nil then
- visit_inorder(node.right)
- end
- end
- return coroutine.wrap(
- function() visit_inorder(root) end
- )
- end
- -- 計算元素總和
- for e in tree_iterator(binary_tree)
- do
- print(e)
- end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
1
3
4
5
5.5
7
7.4
9
11
>Exit code: 0
coroutine迭代器例子 -- 字符串分段
- local function SegIter(instr, segSize)
- local strIndex =
- local function GetSegStr(str)
- 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
- coroutine.yield(strseg)
- GetSegStr(str)
- end
- end
- local co = coroutine.create(function()
- GetSegStr(instr)
- end)
- -- iterator
- return function()
- local code, res = coroutine.resume(co)
- if not code then return nil end
- return res
- end
- end
- for element in SegIter("", )
- do
- print(element)
- end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
66
66
66
66
66
66
66
66
6
>Exit code: 0
注意两个协程执行的主体函数都有递归调用的影子。 这个方法, 保证每个递归中的 yield 都能够执行, 知道最后yield执行完毕后, 函数执行完毕, 返回nil。
lua coroutine for iterator的更多相关文章
- Lua Coroutine详解
协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈,局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西.线程与协同程序的主要区别在于,一个具有多线程的程序可以同时运行几个线程 ...
- 【转】Lua coroutine 不一样的多线程编程思路
Lua coroutine 不一样的多线程编程思路 Sunday, Apr 26th, 2009 by Tim | Tags: coroutine, Lua 上周末开始看<Lua程序设计> ...
- lua coroutine
Lua中协程都放在表coroutine中. Lua协程的四个状态 挂起(suspended):一个协程被创建的时候,处于挂起状态,不会自动运行. 运行(running):coroutine.resum ...
- C/C++ Lua Parsing Engine
catalog . Lua语言简介 . 使用 Lua 编写可嵌入式脚本 . VS2010编译Lua . 嵌入和扩展: C/C++中执行Lua脚本 . 将C++函数导出到Lua引擎中: 在Lua脚本中执 ...
- Nginx+lua+openresty精简系列
1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...
- 生成lua的静态库.动态库.lua.exe和luac.exe
前些日子准备学习下关于lua coroutine更为强大的功能,然而发现根据lua 5.1.4版本来运行一段代码的话也会导致 "lua: attempt to yield across me ...
- 理解 Lua 的那些坑爹特性
按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...
- 跨平台高效率Lua网络库 ( 同步形式的API ,底层是异步非阻塞)
Joynet 项目地址:https://github.com/IronsDu/Joynet 介绍 high performance network library for lua, based on ...
- Openresty Lua协程调度机制
写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...
随机推荐
- MIT 6.828 JOS学习笔记9. Exercise 1.5
Lab 1 Exercise 5 再一次追踪一下boot loader的一开始的几句指令,找到第一条满足如下条件的指令处: 当我修改了boot loader的链接地址,这个指令就会出现错误. 找到这样 ...
- SQLite 创建自增长标识列
SQLite Autoincrement(自动递增) SQLite 的 AUTOINCREMENT 是一个关键字,用于表中的字段值自动递增.我们可以在创建表时在特定的列名称上使用 AUTOINCREM ...
- 判断IP地址是否合法类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IPFl ...
- three.js自定义形状
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- NIO的一些相关链接
Architecture of a Highly Scalable NIO-Based Server Scalable IO in Java Tricks and Tips with NIO part ...
- 【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识
自从Cocos2d-x3.0开始,Cocos2dx就正式的使用了C++11标准.C++11简洁方便的特性使程序的可拓展性和可维护性大大提高,也提高了代码的书写速度. 下面我们就来一起学习一下Cocos ...
- CentOS两台服务器利用scp拷贝文件
yum install -y openssh-clients scp -r -P 26611 /usr/local/ssdb-20160518/ root@10.10.6.199:/usr/local ...
- Graphviz从入门到不精通
1.安装Graphviz (windows 版本,后面说linux下的安装) 1.1)下载安装文件 从graphviz官网下载 http://www.graphviz.org/Download.php ...
- crontab中执行任务定位到秒级
每秒执行一次: * * * * * /bin/sleep 1 ; echo "1"