Created
January 19, 2014 13:29
-
-
Save nadako/8505003 to your computer and use it in GitHub Desktop.
stack-allocated pair iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Main.main = function() { | |
var a = ["a","b","c"]; | |
var _g_a = a; | |
var _g_i = 0; | |
while(_g_i < _g_a.length) { | |
var p_idx = _g_i; | |
var p_val = _g_a[_g_i++]; | |
console.log(p_idx); | |
console.log(p_val); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main | |
{ | |
static function main() | |
{ | |
var a = ["a", "b", "c"]; | |
for (p in new IndexIter(a)) | |
{ | |
trace(p.idx); | |
trace(p.val); | |
} | |
} | |
} | |
class IndexIter<T> | |
{ | |
var a:Array<T>; | |
var i:Int; | |
public inline function new(arr:Array<T>) | |
{ | |
a = arr; | |
i = 0; | |
} | |
public inline function hasNext() return i < a.length; | |
public inline function next():Pair<T> return {idx: i, val: a[i++]}; | |
} | |
typedef Pair<T> = {idx:Int, val:T}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment