فهرست منبع

[lua] add pairtools iterators

Justin Donaldson 8 سال پیش
والد
کامیت
33040ad03d
1فایلهای تغییر یافته به همراه32 افزوده شده و 0 حذف شده
  1. 32 0
      std/lua/PairTools.hx

+ 32 - 0
std/lua/PairTools.hx

@@ -55,4 +55,36 @@ class PairTools {
 		untyped __lua__("for k,v in _G.pairs(table1) do ret[k] = v end");
 		return ret;
 	}
+
+	public static function pairsIterator<A,B>(table:Table<A,B>) : Iterator<{index:A, value:B}> {
+		var p = Lua.pairs(table);
+		var next = p.next;
+		var i = p.index;
+		return {
+			next : function(){
+				var res = next(table,i);
+				i = res.index;
+				return {index : res.index, value : res.value};
+			},
+			hasNext : function(){
+				return Lua.next(table, i).value != null;
+			}
+		}
+	}
+
+	public static function ipairsIterator<A,B>(table:Table<A,B>) : Iterator<{index:Int, value:B}> {
+		var p = Lua.ipairs(table);
+		var next = p.next;
+		var i = p.index;
+		return {
+			next : function(){
+				var res = next(table,i);
+				i = res.index;
+				return {index : res.index, value : res.value};
+			},
+			hasNext : function(){
+				return next(table, i).value != null;
+			}
+		}
+	}
 }