lua自定義迭代器

迭代器

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

迭代器能夠讓你遍歷某個集合或者容器中的每一個元素。 對于lua來說, 集合通常指代 table, 用于創建變化的數據結構, 類似數組。

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循環, 配合in使用, in后的參數 就是一個迭代器函數。

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

對于迭代器函數, 根據迭代器函數中狀態的維護, 可以分為如下兩種類型, 有狀態迭代器, 和 無狀態迭代器。

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

?

無狀態迭代器

迭代器函數中不維護任何狀態。

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.

如下例子, 迭代狀態, 由for的參數 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

?

改進版:

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

?

有狀態迭代器

利用閉包,將狀態管理在閉包類, 迭代器函數為閉包。

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

?

自定義例子

使用有狀態迭代器, 實現字符串拆分為固定長度的字符串:

?

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

?

轉載于:https://www.cnblogs.com/lightsong/p/5840399.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457319.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457319.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457319.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql非主鍵索引_主鍵索引和非主鍵索引的區別

1. 什么是最左前綴原則&#xff1f;以下回答全部是基于MySQL的InnoDB引擎例如對于下面這一張表如果我們按照 name 字段來建立索引的話&#xff0c;采用B樹的結構&#xff0c;大概的索引結構如下如果我們要進行模糊查找&#xff0c;查找name 以“張"開頭的所有人的ID&#…

優美的配色方案設計

2019獨角獸企業重金招聘Python工程師標準>>> 怎么做好設計配色一直是個難題&#xff0c;雖然網站上有各種各樣的色庫&#xff0c;但配色仍然至關重要&#xff0c;不得已的話可以親自動手&#xff0c;況且樂趣滿滿。 這個沒有一套標準&#xff0c;所以看自己怎么喜歡…

It's a start!

開始博客之旅轉載于:https://www.cnblogs.com/catchingdream/p/5843172.html

mysql死鎖釋放時間參數_【Mysql】mysql 事務未提交導致死鎖 Lock wait timeout exceeded; try restarting transaction 解決辦法...

問題場景問題出現環境&#xff1a;1、在同一事務內先后對同一條數據進行插入和更新操作&#xff1b;2、多臺服務器操作同一數據庫&#xff1b;3、瞬時出現高并發現象&#xff1b;不斷的有一下異常拋出&#xff0c;異常信息&#xff1a;org.springframework.dao.CannotAcquireLo…

ORACLE sqlplus設置行數和寬度

1) 查看目前的pagesize,默認是14:Sqlplus代碼show pagesize; 2) 將pagesize設置好100,則可以一次顯示夠多行記錄了:Sqlplus代碼set pagesize 100; 2. 設置行的寬度1) 查看目前的linesize,默認是80:Sqlplus代碼show linesize; 2) 設置成100或者更寬都可以:Sqlplus代碼set li…

mysql關系模式怎么畫_關系數據庫與mysql

表下面是阿里的mysql設計原則&#xff0c;可以參考&#xff0c;不一定按照阿里規則&#xff0c;但一個團隊一定要有規則&#xff0c;如果現在沒有規則&#xff0c;從現在開始&#xff0c;慢慢推廣&#xff0c;適應1.【強制】表達是與否概念的字段&#xff0c;必須使用 is_xxx的…

Javascript 構造函數模式、原型模式

前兩天寫完組合繼承&#xff0c;打算總結一下原型繼承的&#xff0c;不過今天看了一下工廠模式、構造函數模式和原型模式&#xff0c;覺得有必要總結一下以加深印象。 ———————————————————————————————————————————————————…

2016年CCF第七次測試 俄羅斯方塊

1 //2016年CCF第七次測試 俄羅斯方塊2 // 這道小模擬題還是不錯3 // 思路&#xff1a;處理出輸入矩陣中含1格子的行數和列數4 // 再判是否有一個格子碰到底部&#xff0c;否則整體再往下移動一步&#xff0c;如果有一個格子不能移動&#xff0c;要返回到前一步5 6 #include <…

springmvc視圖解析器_SpringMVC視圖及REST風格

什么是視圖解析器&#xff1f;springMVC用于處理視圖最重要的兩個接口是ViewResolver和View。ViewResolver的主要作用是把一個邏輯上的視圖名稱解析成一個真的的視圖&#xff0c;而SpringMVC中用于把View對象呈現給客戶端的是View對象本身&#xff0c;而ViewResolver只是把邏輯…

mysql5.7.x 1251_MySql-8.0.x免安裝版下載與配置,Navicat打開數據庫鏈接報錯1251的解決辦法...

概述MySQL從5.7一下子跳到了MySQL8.0, 其中的變化必然是很大的, 這里就不說了, 本文主要講解最新版MySQL安裝的事情.實際上5.7版本后的mysql免安裝版都是沒有data文件和my.ini文件的&#xff0c;下面再具體說明怎么生成&#xff0c;注意不能自己手動新建.下載下載程序必然去官網…

To install 64-bit ODBC drivers

為了更充分的利用硬件資源&#xff0c;我想很多人都開使用64位操作系統了&#xff0c;同時你可以也發現了在64位操作系統上ODBC的驅動找不到了&#xff0c;所以ODBC的東西都沒法用了。 因為2007以前版本的Office只有32位版本&#xff0c;所以我們不能在64位系統上使用ODBC。使用…

【Qt開發】QTableWidget設置根據內容調整列寬和行高

QTableWidget要調整表格行寬主要涉及以下一個函數 1.resizeColumnsToContents(); 根據內容調整列寬 2.resizeColumnToContents(int col); 根據內容自動調整給定列寬 3.horizontalHeader()->setResizeMode 把給定列…

深入淺出mysql數據開發_深入淺出MySQL數據庫開發、優化與管理維護 PDF掃描版[513KB]...

深入淺出MySQL數據庫開發、優化與管理維護 內容介紹&#xff1a;本書從數據庫的基礎、開發、優化、管理維護4個方面對MySQL進行了詳細的介紹&#xff0c;其中每一部分都獨立成篇。本書內容實用&#xff0c;覆蓋廣泛&#xff0c;講解由淺入深&#xff0c;適合于各個層次的讀者。…

Understand Lambda Expressions in 3 minutes(翻譯)

本文翻譯自CodeProject上的一篇簡單解釋Lambda表達式的文章&#xff0c;適合新手理解。譯文后面我補充了一點對Lambda表達式的說明。 1.什么是Lambda表達式&#xff1f; Lambda表達式是一種匿名方法&#xff0c;多數情況下用來在LINQ中快速創建委托。簡單地說&#xff0c;它代表…

Hibernate二級緩存配置

一、定義&#xff1a; 二級緩存是進程或集群范圍內的緩存&#xff0c;可以被所有的Session共享&#xff0c;是可配置的插件 二、二級緩存原理圖 解析&#xff1a;每次從二級緩存中取出的對象&#xff0c;都是一個新的對象。 三、配置步驟如下&#xff1a; 同理&#xff1a;以員…

redis配置主從沒效果_跟我一起學Redis之加個哨兵讓主從復制更加高可用

Redis哨兵(Sentinel)其實本質就是一個RedisServer節點&#xff0c;通過設置 運行模式 來開啟哨兵的功能&#xff1b;主要功能如下&#xff1a;監控(Monitoring )&#xff1a;哨兵節點會不斷地檢查的主服務和從服務的運行狀態&#xff1b;自動故障遷移(Automatic failover) &…

閏秒導致MySQL服務器的CPU sys過高

今天&#xff0c;有個哥們碰到一個問題&#xff0c;他有一個從庫&#xff0c;只要是啟動MySQL&#xff0c;CPU使用率就非常高&#xff0c;其中sys占比也比較高&#xff0c;具體可見下圖。 注意&#xff1a;他的生產環境是物理機&#xff0c;單個CPU&#xff0c;4個Core。 于是&…

position定位——讓人又愛又恨的屬性

關于css中的position這個屬性&#xff0c;在使用的時候&#xff0c;有時很強大&#xff0c;有時又讓人很無奈。 強大的時候&#xff0c;對于div中的一些小物件不方便使用margin或者padding的時候&#xff0c;給與position:absolute;再配備left、right、top和bottom&#xff0c;…

CentOS 6.8安裝Python2.7.13

轉載自&#xff1a;http://www.cnblogs.com/94YY/p/6224441.html查看當前系統中的 Python 版本python --version返回 Python 2.6.6 為正常。檢查 CentOS 版本cat /etc/redhat-release返回 CentOS release 6.8 (Final) 為正常。安裝所有的開發工具包yum groupinstall -y "D…

新安裝數據庫sqlserver2008r2,使用javaweb連接不上問題處理

鼠標右鍵【計算機】--》【管理】&#xff0c;打開界面如下&#xff1a; 選擇自己數據庫的實例名&#xff1a; 選擇TCP/IP&#xff1a;右鍵【屬性】&#xff0c;將所有TCP動態端口的【0】刪掉&#xff0c;TCP端口設為1433&#xff1b;重啟服務&#xff0c;即可連接。PS:不知道這…