日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

lua自定义迭代器

發(fā)布時(shí)間:2023/12/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lua自定义迭代器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

迭代器

http://www.tutorialspoint.com/lua/lua_iterators.htm

迭代器能夠讓你遍歷某個(gè)集合或者容器中的每一個(gè)元素。 對(duì)于lua來說, 集合通常指代 table, 用于創(chuàng)建變化的數(shù)據(jù)結(jié)構(gòu), 類似數(shù)組。

Iterator is a construct that enables you to traverse through the elements of the so called collection or container. In Lua, these collections often refer to tables, which are used to create various data structures like array.

?

通用For迭代器

?

通常使用的for循環(huán), 配合in使用, in后的參數(shù) 就是一個(gè)迭代器函數(shù)。

A generic for iterator provides the key value pairs of each element in the collection. A simple example is given below.

array = {"Lua", "Tutorial"} for key,value in ipairs(array) do print(key, value) end

對(duì)于迭代器函數(shù), 根據(jù)迭代器函數(shù)中狀態(tài)的維護(hù), 可以分為如下兩種類型, 有狀態(tài)迭代器, 和 無狀態(tài)迭代器。

In Lua we use functions to represent iterators. Based on the state maintenance in these iterator functions, we have two main types ?

  • Stateless Iterators
  • Stateful Iterators

?

無狀態(tài)迭代器

迭代器函數(shù)中不維護(hù)任何狀態(tài)。

By the name itself we can understand that this type of iterator function does not retain any state.

Let us now see an example of creating our own iterator using a simple function that prints the squares of n numbers.

如下例子, 迭代狀態(tài), 由for的參數(shù) i 記錄。

function square(iteratorMaxCount,currentNumber) if currentNumber<iteratorMaxCount then currentNumber = currentNumber+1 return currentNumber, currentNumber*currentNumber end end for i,n in square,3,0 do print(i,n) end

?

改進(jìn)版:

function square(iteratorMaxCount,currentNumber) if currentNumber<iteratorMaxCount then currentNumber = currentNumber+1 return currentNumber, currentNumber*currentNumber end end function squares(iteratorMaxCount) return square,iteratorMaxCount,0 end for i,n in squares(3) do print(i,n) end

?

有狀態(tài)迭代器

利用閉包,將狀態(tài)管理在閉包類, 迭代器函數(shù)為閉包。

The previous example of iteration using function does not retain the state. Each time the function is called, it returns the next element of the collection based on a second variable sent to the function. To hold the state of the current element, closures are used. Closure retain variables values across functions calls. To create a new closure, we create two functions including the closure itself and a factory, the function that creates the closure.

Let us now see an example of creating our own iterator in which we will be using closures.

?

如下例子,推薦使用如下寫法,減少信息暴漏:

array = {"Lua", "Tutorial"} function elementIterator (collection) local index = 0 local count = #collection -- The closure function is returned return function () index = index + 1 if index <= count then -- return the current element of the iterator return collection[index] end end end for element in elementIterator(array) do print(element) end

?

自定義例子

使用有狀態(tài)迭代器, 實(shí)現(xiàn)字符串拆分為固定長(zhǎng)度的字符串:

?

local instr = "2334t545dfgjkkkk"function StrSegIterator (str, segSize)local strIndex = 1-- The closure function is returnedreturn function ()local segStart = strIndexlocal segEnd = strIndex + segSize - 1local strseg = string.sub(str, segStart, segEnd)if #strseg > 0 thenstrIndex = strIndex + segSize-- return the current element of the iteratorreturn strsegendendendfor element in StrSegIterator(instr, 2) doprint(element) end

?

轉(zhuǎn)載于:https://www.cnblogs.com/lightsong/p/5840399.html

總結(jié)

以上是生活随笔為你收集整理的lua自定义迭代器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。