Created
May 17, 2019 15:30
-
-
Save markknol/fe42c9ee23d566a5eb53504a6f3c763f to your computer and use it in GitHub Desktop.
Support `for(index => value in array)` syntax in Haxe
This file contains 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 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 ++] }; | |
} |
This file contains 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
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