Last active
October 14, 2016 10:35
-
-
Save RealyUniqueName/2fbeb1ec17e637d6eaf50b0facde206f to your computer and use it in GitHub Desktop.
Key-value map iterator
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 KeyValueIterator<K,V> { | |
var map:Map<K,V>; | |
var keys:Iterator<K>; | |
static public inline function pairs<K,V>(map:Map<K,V>) return new KeyValueIterator(map); | |
public inline function new(map:Map<K,V>) { | |
this.map = map; | |
this.keys = map.keys(); | |
} | |
public inline function hasNext() return keys.hasNext(); | |
public inline function next() { | |
var key = keys.next(); | |
return new KeyValuePair(key, map.get(key)); | |
} | |
} | |
class KeyValuePair<K,V> { | |
public var key (default,null):K; | |
public var value (default,null):V; | |
public inline function new(key:K, value:V) { | |
this.key = key; | |
this.value = value; | |
} | |
} |
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 KeyValueIterator; | |
class Test { | |
static function main() { | |
var map = ['Hello' => 'World', 'Bye' => 'World']; | |
//using key-value iterator | |
for(pair in map.pairs()) { | |
trace(pair.key + pair.key); | |
trace(pair.value + pair.value); | |
} | |
//"classic" workflow | |
for(key in map.keys()) { | |
var value = map.get(key); | |
trace(key + key); | |
trace(value + value); | |
} | |
} | |
} |
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 key-value iterator | |
var map = _g; | |
var _g1_keys = map.keys(); | |
while(_g1_keys.hasNext()) { | |
var key = _g1_keys.next(); | |
var _this = _g; | |
var value = __map_reserved[key] != null?_this.getReserved(key):_this.h[key]; | |
console.log(key + key); | |
console.log(value + value); | |
} | |
//"classic" workflow | |
var tmp = _g.keys(); | |
while(tmp.hasNext()) { | |
var key1 = tmp.next(); | |
var value1 = __map_reserved[key1] != null?_g.getReserved(key1):_g.h[key1]; | |
console.log(key1 + key1); | |
console.log(value1 + value1); | |
} | |
//<...> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should be added to the std lib 👍