Skip to content

Instantly share code, notes, and snippets.

@nadako
Created January 19, 2014 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadako/8505003 to your computer and use it in GitHub Desktop.
Save nadako/8505003 to your computer and use it in GitHub Desktop.
stack-allocated pair iterator
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);
}
};
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