背景

前面的文章演示了使用闭包函数实现 状态的迭代器。

本文演示使用 coroutine来产生迭代器的例子。

coroutine迭代器例子 -- 遍历二叉树

  1. local binary_tree = {
  2. data = ,
  3. left = {
  4. data = ,
  5. left = { data = },
  6. right = { data = }
  7. },
  8. right = {
  9. data = ,
  10. left = {
  11. data = ,
  12. left = { data = 5.5 },
  13. right = { data = 7.4}
  14. },
  15. right = { data = }
  16. }
  17. }
  18.  
  19. function tree_iterator(root)
  20. local function visit_inorder(node)
  21. if node.left ~= nil then
  22. visit_inorder(node.left)
  23. end
  24.  
  25. coroutine.yield(node.data)
  26.  
  27. if node.right ~= nil then
  28. visit_inorder(node.right)
  29. end
  30. end
  31.  
  32. return coroutine.wrap(
  33. function() visit_inorder(root) end
  34. )
  35. end
  36.  
  37. -- 計算元素總和
  38. for e in tree_iterator(binary_tree)
  39. do
  40. print(e)
  41. 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迭代器例子 -- 字符串分段

  1. local function SegIter(instr, segSize)
  2. local strIndex =
  3.  
  4. local function GetSegStr(str)
  5.  
  6. local segStart = strIndex
  7. local segEnd = strIndex + segSize -
  8. local strseg = string.sub(str, segStart, segEnd)
  9.  
  10. if #strseg > then
  11. strIndex = strIndex + segSize
  12.  
  13. -- return the current element of the iterator
  14. coroutine.yield(strseg)
  15.  
  16. GetSegStr(str)
  17. end
  18. end
  19.  
  20. local co = coroutine.create(function()
  21. GetSegStr(instr)
  22. end)
  23.  
  24. -- iterator
  25. return function()
  26. local code, res = coroutine.resume(co)
  27. if not code then return nil end
  28. return res
  29. end
  30. end
  31.  
  32. for element in SegIter("", )
  33. do
  34. print(element)
  35. 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的更多相关文章

  1. Lua Coroutine详解

    协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈,局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西.线程与协同程序的主要区别在于,一个具有多线程的程序可以同时运行几个线程 ...

  2. 【转】Lua coroutine 不一样的多线程编程思路

    Lua coroutine 不一样的多线程编程思路 Sunday, Apr 26th, 2009 by Tim | Tags: coroutine, Lua 上周末开始看<Lua程序设计> ...

  3. lua coroutine

    Lua中协程都放在表coroutine中. Lua协程的四个状态 挂起(suspended):一个协程被创建的时候,处于挂起状态,不会自动运行. 运行(running):coroutine.resum ...

  4. C/C++ Lua Parsing Engine

    catalog . Lua语言简介 . 使用 Lua 编写可嵌入式脚本 . VS2010编译Lua . 嵌入和扩展: C/C++中执行Lua脚本 . 将C++函数导出到Lua引擎中: 在Lua脚本中执 ...

  5. Nginx+lua+openresty精简系列

    1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...

  6. 生成lua的静态库.动态库.lua.exe和luac.exe

    前些日子准备学习下关于lua coroutine更为强大的功能,然而发现根据lua 5.1.4版本来运行一段代码的话也会导致 "lua: attempt to yield across me ...

  7. 理解 Lua 的那些坑爹特性

    按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...

  8. 跨平台高效率Lua网络库 ( 同步形式的API ,底层是异步非阻塞)

    Joynet 项目地址:https://github.com/IronsDu/Joynet 介绍 high performance network library for lua, based on  ...

  9. Openresty Lua协程调度机制

    写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...

随机推荐

  1. MIT 6.828 JOS学习笔记9. Exercise 1.5

    Lab 1 Exercise 5 再一次追踪一下boot loader的一开始的几句指令,找到第一条满足如下条件的指令处: 当我修改了boot loader的链接地址,这个指令就会出现错误. 找到这样 ...

  2. SQLite 创建自增长标识列

    SQLite Autoincrement(自动递增) SQLite 的 AUTOINCREMENT 是一个关键字,用于表中的字段值自动递增.我们可以在创建表时在特定的列名称上使用 AUTOINCREM ...

  3. 判断IP地址是否合法类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IPFl ...

  4. three.js自定义形状

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  5. 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 ...

  6. NIO的一些相关链接

    Architecture of a Highly Scalable NIO-Based Server Scalable IO in Java Tricks and Tips with NIO part ...

  7. 【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识

    自从Cocos2d-x3.0开始,Cocos2dx就正式的使用了C++11标准.C++11简洁方便的特性使程序的可拓展性和可维护性大大提高,也提高了代码的书写速度. 下面我们就来一起学习一下Cocos ...

  8. CentOS两台服务器利用scp拷贝文件

    yum install -y openssh-clients scp -r -P 26611 /usr/local/ssdb-20160518/ root@10.10.6.199:/usr/local ...

  9. Graphviz从入门到不精通

    1.安装Graphviz (windows 版本,后面说linux下的安装) 1.1)下载安装文件 从graphviz官网下载 http://www.graphviz.org/Download.php ...

  10. crontab中执行任务定位到秒级

    每秒执行一次: * * * * * /bin/sleep 1 ; echo "1"