Lua學習筆記--迭代器和協程(二)
迭代器
function iterator(tab)
local i=0
return function()
i=i+1;
return tab[i]
end
end
t={10,20,30}
it=iterator(t)
while true do
local element=it()
if element==nil then break end
print(element)
end
--另外一種呼叫方式
for element in iterator(t) do
print(element)
end
協程
--相當於建立了Runnable物件
co=coroutine.create(
function()
print("hello world")
end
)
--物件建立之後,協程的狀態是掛起,並沒有執行
print(coroutine.status(co))
--執行程式.
coroutine.resume(co)
已經執行完成的協程,是死亡狀態,不能再次執行.
但是yield可以將程式由執行狀態,轉為掛起狀態.
co=coroutine.create(
function()
for i=1,3 do
print("co",i)
coroutine.yield()
end
end
)
print(coroutine.status(co))
coroutine.resume(co)
print(coroutine.status(co))
coroutine.resume(co)
coroutine.resume(co)
[nginx@localhost~]$./lua_test.lua
suspended
co 1
suspended
co 2
co 3
管道
function receive()
local status,value=coroutine.resume(producer)
print("receive:"..value)
return value
end
function send(x)
print("send:"..x)
coroutine.yield(x)
end
producer=coroutine.create(
function()
while true do
local x=io.read()
send(x)
end
end
)
consumter=coroutine.create(
function()
while true do
local x=receive()
io.write(x,"\n")
end
end
)
coroutine.resume(consumter)
過濾器
function receive(prod)
local status,value=coroutine.resume(prod)
return value
end
function send(x)
coroutine.yield(x)
end
function producer()
return
coroutine.create(
function()
while true do
local x=io.read()
send(x)
end
end
)
end
function filter(prod)
return coroutine.create(function()
for line=1,math.huge do
local x=receive(prod)
x=string.format("%5d: %s",line,x)
send(x)
end
end
)
end
function consumter(prod)
while true do
local x=receive(prod)
io.write(x,"\n")
end
end
p=producer()
f=filter(p)
consumter(f)
function iterator(tab)
local i=0
return function()
i=i+1;
return tab[i]
end
end
t={10,20,30}
it=iterator(t)
while true do
local element=it()
if element==nil then break end
print(element)
end
--另外一種呼叫方式
for element in iterator(t) do
print(element)
end
協程
--相當於建立了Runnable物件
co=coroutine.create(
function()
print("hello world")
end
)
--物件建立之後,協程的狀態是掛起,並沒有執行
print(coroutine.status(co))
--執行程式.
coroutine.resume(co)
已經執行完成的協程,是死亡狀態,不能再次執行.
但是yield可以將程式由執行狀態,轉為掛起狀態.
co=coroutine.create(
function()
for i=1,3 do
print("co",i)
coroutine.yield()
end
end
)
print(coroutine.status(co))
coroutine.resume(co)
print(coroutine.status(co))
coroutine.resume(co)
coroutine.resume(co)
[nginx@localhost~]$./lua_test.lua
suspended
co 1
suspended
co 2
co 3
function receive()
local status,value=coroutine.resume(producer)
print("receive:"..value)
return value
end
function send(x)
print("send:"..x)
coroutine.yield(x)
end
producer=coroutine.create(
function()
while true do
local x=io.read()
send(x)
end
end
)
consumter=coroutine.create(
function()
while true do
local x=receive()
io.write(x,"\n")
end
end
)
coroutine.resume(consumter)
過濾器
function receive(prod)
local status,value=coroutine.resume(prod)
return value
end
function send(x)
coroutine.yield(x)
end
function producer()
return
coroutine.create(
function()
while true do
local x=io.read()
send(x)
end
end
)
end
function filter(prod)
return coroutine.create(function()
for line=1,math.huge do
local x=receive(prod)
x=string.format("%5d: %s",line,x)
send(x)
end
end
)
end
function consumter(prod)
while true do
local x=receive(prod)
io.write(x,"\n")
end
end
p=producer()
f=filter(p)
consumter(f)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1850026/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- gevent 學習筆記 —— 協程筆記
- Lua學習筆記筆記
- golang學習筆記(二)—— 深入golang中的協程Golang筆記
- Python學習筆記 - 迭代器和生成器Python筆記
- Lua 學習筆記(上)筆記
- Lua學習筆記(一)筆記
- unity學習筆記-C#協程Unity筆記C#
- 協程與迭代器
- Lua5.3學習筆記筆記
- 設計模式學習筆記之迭代器模式設計模式筆記
- Python學習之迭代器協議Python協議
- angular學習筆記(七)-迭代1Angular筆記
- angular學習筆記(七)-迭代2Angular筆記
- angular學習筆記(七)-迭代3Angular筆記
- 熱更新語言--lua學習筆記筆記
- Lua學習筆記--物件導向(三)筆記物件
- swoole 學習筆記-做一頓飯來理解協程筆記
- Raft協議學習筆記Raft協議筆記
- Raft 協議學習筆記Raft協議筆記
- OS學習筆記三:處理器排程筆記
- 以前的學習筆記整理:第二層交換和生成樹協議(STP)筆記協議
- Python零基礎學習筆記(二十三)——迭代器Python筆記
- ANFIS學習筆記(二)筆記
- activiti學習筆記二筆記
- Typescript學習筆記(二)TypeScript筆記
- JavaScript學習筆記(二)JavaScript筆記
- React 學習筆記【二】React筆記
- goLang學習筆記(二)Golang筆記
- vue學習筆記二Vue筆記
- vue學習筆記(二)Vue筆記
- 科二學習筆記筆記
- jQuery 學習筆記(二)jQuery筆記
- git學習筆記(二)Git筆記
- Java學習筆記二Java筆記
- Lua學習(二)物件導向物件
- TCP/IP學習筆記之協議和郵件TCP筆記協議
- Python學習筆記|Python之索引迭代Python筆記索引
- 【numpy學習筆記】陣列的切片,索引,迭代筆記陣列索引