Skip to content

Instantly share code, notes, and snippets.

@RealyUniqueName
Last active October 14, 2016 10:35
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RealyUniqueName/2fbeb1ec17e637d6eaf50b0facde206f to your computer and use it in GitHub Desktop.
Save RealyUniqueName/2fbeb1ec17e637d6eaf50b0facde206f to your computer and use it in GitHub Desktop.
Key-value map iterator
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;
}
}
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);
}
}
}
//<...>
//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);
}
//<...>
@kevinresol
Copy link

This should be added to the std lib 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment