Programming in Lua | ||
| Part I. The Language Chapter 7. Iterators and the Generic for |
7.4 - Iterators with Complex State
Frequently, an iterator needs to keep more state than fits into
a single invariant state and a control variable.
The simplest solution is to use closures.
An alternative solution is to pack all it needs into a table
and use this table as the invariant state for the iteration.
Using a table, an iterator can keep as much data as it needs
along the loop.
Moreover, it can change that data as it goes.
Although the state is always the same table
(and therefore invariant),
the table contents change along the loop.
Because such iterators have all their data in the state,
they typically discard the second argument
provided by the generic for
(the iterator variable).
As an example of this technique,
we will rewrite the iterator allwords,
which traverses all the words from the current input file.
This time, we will keep its state using a table with two fields,line and pos.
The function that starts the iteration is simple.
It must return the iterator function and the initial state:
local iterator -- to be defined later
function allwords ()
local state = {line = io.read(), pos = 1}
return iterator, state
end
The
iterator function does the real work:
function iterator (state)
while state.line do -- repeat while there are lines
-- search for next word
local s, e = string.find(state.line, "%w+", state.pos)
if s then -- found a word?
-- update next position (after this word)
state.pos = e + 1
return string.sub(state.line, s, e)
else -- word not found
state.line = io.read() -- try next line...
state.pos = 1 -- ... from first position
end
end
return nil -- no more lines: end loop
end
Whenever it is possible,
you should try to write stateless iterators,
those that keep all their state in the for variables.
With them, you do not create new objects when you start a loop.
If you cannot fit your iteration into that model,
then you should try closures.
Besides being more elegant,
typically a closure is more efficient
than an iterator using tables:
First, it is cheaper to create a closure than a table;
second, access to upvalues is faster than
access to table fields.
Later we will see yet another way to write iterators,
with coroutines.
This is the most powerful solution,
but a little more expensive.
Programming in Lua |
Always read stuff that will make you look good if you die in the middle of it.............................................
ReplyDelete台灣性樂園台灣色情論壇三點全裸台灣成人色情網色情站美女脫胸罩愛愛圖片台灣色情成人網站辣妹妹影音視訊聊天室情色區g罩杯美女av性愛女生自慰圖成人交友網站色情區丁字褲貼圖成人視訊聊天hi sex台灣十八成人丁字褲自拍太太陰毛火辣美眉成人色情圖片網視訊打槍美女聊天一夜激情一夜情聊天網成人聊天網激情成人聊天室上床圖片火辣情色辣妹聊天美女色情情趣丁字褲亞洲情色論壇情色天空三點全露內衣秀台灣色情網站成人裸照av成人論壇成人文學
ReplyDelete成人文學
Never put both feet in your mouth at the same time, because then you will not have a leg to stand on.............................................
ReplyDelete夫妻私密俱樂部素人自拍無碼女優自拍密錄館正妹無名相簿正妹裸照正妹桌面正妹桌布成人 影片星野亞希 h星野亞季星野亞西主題星野亞西樣式星野亞西桌布星野亞矢星野涼宇星野美由星野akari星空網路聊天室視訊 辣妹go2av免費影片航海王h短片分享go2av免費看影片go2av免費影片18 禁一葉情貼影入口18 成人girl5320免費影片18成人咆哮小老鼠視訊聊天免費成人片免費a片觀看真人野球拳免費聊天交友ut視訊080aa片直播台灣18網免費A片柔情聊天網一對多視訊聊天人妻貼圖
ReplyDelete學問好比腸胃裡的食物,裝下多少並不重要,吸收多少才重要。......................................................
ReplyDeleteNice Post~!!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ReplyDelete很高興見到你哦!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ReplyDelete格主的部落格內容真豐富~~看得很開心..................................................
ReplyDelete世間是非,要如水泥地般水過則乾。...............................................................
ReplyDelete完成一個小目標,會把自己推向一個大目標.............................................
ReplyDelete任何事都是由一個決心,一顆種子開始。..................................................
ReplyDeleteWhere theres a will theres a way. ............................................................
ReplyDeleteMore haste, less speed............................................................................
ReplyDelete如果成為一支火柴,也要點亮一個短暫的宇宙;如果是一隻烏鴉,也要叫疼閉塞的耳膜。..................................................
ReplyDelete要持續更新下去喲!!祝你心情愉快.................................................................
ReplyDelete嘿,你的部落格不錯耶~~只是想跟您問聲好!! .................................................................
ReplyDelete一棵樹除非在春天開了花,否則難望在秋天結果。....................................................................
ReplyDelete一棵樹除非在春天開了花,否則難望在秋天結果。....................................................................
ReplyDelete人不能像動物一樣活著,而應該追求知識和美德............................................................
ReplyDelete人生是故事的創造與遺忘。............................................................
ReplyDelete唯有學習不已的老師,才能認真的教,唯有燃燒自己,才能點亮他人的燈............................................................
ReplyDelete第一次來這裡 愛上你的部落格 感謝你的分享............................................................
ReplyDelete河水永遠是相同的,可是每一剎那又都是新的。..................................................
ReplyDeleteNever hesitate to hold out your hand; never hesitate to accept the outstretched hand of another...................................................................
ReplyDelete百發百中不是一試就成的。..................................................
ReplyDelete[做人難,人難做,難做人] 人.事的艱困與磨難,是一種考驗!要以樂觀歡喜之心,很珍惜地過每一天!^^..................................................................
ReplyDeleteQuality is better than quantity.............................................................
ReplyDelete第一忠誠,第二勤奮,第三專心工作。..................................................
ReplyDelete初次造訪,感謝你的分享..................................................................
ReplyDeletePay somebody back in his own coin...................................................................
ReplyDelete良言一句三冬暖,惡語傷人六月寒。......................................................................
ReplyDelete真得很不錯的blog,加油哦............................................................
ReplyDelete謝謝大大分享!!經典!~(。・ω・)............................................................
ReplyDelete你的分享很不錯.. 謝謝 ..................................................
ReplyDelete教育的目的,不在應該思考什麼,而是教吾人怎樣思考......................................................................
ReplyDelete