Skip to content

Instantly share code, notes, and snippets.

@markknol
Created May 17, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markknol/fe42c9ee23d566a5eb53504a6f3c763f to your computer and use it in GitHub Desktop.
Save markknol/fe42c9ee23d566a5eb53504a6f3c763f to your computer and use it in GitHub Desktop.
Support `for(index => value in array)` syntax in Haxe
class ArrayUtils {
/**
Support `for(index => value in array)` syntax.
**/
public static inline function keyValueIterator<A>(arr:Array<A>) {
return new ArrayKeyValueIterator(arr);
}
}
private class ArrayKeyValueIterator<T>{
var index:Int = 0;
final array:Array<T>;
public inline function new(array:Array<T>) this.array = array;
public inline function hasNext() return index < array.length;
public inline function next() return { key: index, value: array[index ++] };
}
using ArrayUtils; // ← put this in the import.hx file in root of your project
class Test {
static function main() {
var array = ["a","b","c"];
for(index => value in array) {
trace('index=$index: value=$value');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment