This is a set of frequently used iterators for Haxe collections and other types.
All iterators are designed to have zero impact on runtime performance.
Compatible with Haxe 3.4 and 4.0
haxelib install iterators
usingIterators;
for(iin10.to(0).step(-2)) { //.step() is optionaltrace(i);
}
varstr='hello';
for(cinstr.chars()) {
trace(c);
}Full list of available iterators:
classMapIterators {
/** * Map iterator which allows you to get key & value in one go. * ``` * var m = [1 => 'hello']; * for(p in map.pairs()) { * trace(p.key); // 1 * trace(p.value); // hello * } * ```*/staticpublicinlinefunctionpairs<K,V>(map:Map<K,V>) returnnewMapPairIterator(map);
}
classDynamicAccessIterators {
/** * Iterator for `haxe.DynamicAccess` which allows you to get key & value in one go. * ``` * var obj:DynamicAccess<String> = {hello:'world'}; * for(p in obj.pairs()) { * trace(p.key); // hello * trace(p.value); // world * } * ```*/staticpublicinlinefunctionpairs<V>(obj:DynamicAccess<V>) returnnewKeyValueDynamicAccessIterator(obj);
}
classArrayIterators {
/** * Array iterator which allows you to get index & value in one go. * ``` * var a = ['hello']; * for(p in a.pairs()) { * trace(p.index); // 0 * trace(p.value); // 'hello' * } * ```*/staticpublicinlinefunctionpairs<V>(array:Array<V>) returnnewIndexValueIterator(array);
/** * Array iterator which iterates from the end to the beginning of an array and allows you to get index & value in one go.*/staticpublicinlinefunctionreversePairs<V>(array:Array<V>) returnnewIndexValueReversiveIterator(array);
/** * Array iterator which iterates from the end to the beginning of an array.*/staticpublicinlinefunctionreverseValues<V>(array:Array<V>) returnnewValueReversiveIterator(array);
}
classStringIterators {
/** * String iterator which allows you to get index & character in one go. * ``` * var str = 'hello'; * for(p in str.pairs()) { * trace(p.index); * trace(p.char); * } * ```*/staticpublicinlinefunctionpairs(str:String) returnnewIndexCharIterator(str);
/** * String iterator which iterates from the end to the beginning of a string and allows you to get index & character in one go.*/staticpublicinlinefunctionreversePairs(str:String) returnnewIndexCharReversiveIterator(str);
/** * String iterator over characters. * ``` * var str = 'hello'; * for(c in str.chars()) { * trace(c); // Sequentially prints: h, e, l, l, o * } * ```*/staticpublicinlinefunctionchars(str:String) returnnewCharIterator(str);
/** * String iterator over characters from the end to the beginnning of a string.*/staticpublicinlinefunctionreverseChars(str:String) returnnewCharReversiveIterator(str);
/** * String iterator over codes of characters. * ``` * var str = 'hi'; * for(c in str.charCodes()) { * trace(c); // Sequentially prints: 104, 105 * } * ```*/staticpublicinlinefunctioncharCodes(str:String) returnnewCharCodeIterator(str);
/** * String iterator over codes of characters from the end to the beginnning of a string. * ``` * var str = 'hi'; * for(c in str.charCodes()) { * trace(c); // Sequentially prints: 105, 104 * } * ```*/staticpublicinlinefunctionreverseCharCodes(str:String) returnnewCharCodeReversiveIterator(str);
}
classLengthIterators {
/** * Iterator for values with `length` field which iterates over indices from the end to the beginning of a `value`.*/
@:genericstaticpublicinlinefunctionreverseIndices<T:{varlength(default,never):Int;}>(value:T) returnnewIndexReversiveIterator(value.length);
}
classIntIterators {
/** * Int iterator from `from` (including) to `to` (excluding). * ``` * var value = 10; * for(i in value.to(13)) { * trace(i); // Sequentially prints: 10, 11, 12 * } * for(i in value.to(7)) { * trace(i); // Sequentially prints: 10, 9, 8 * } * for(i in value.to(15).step(2)) { * trace(i); // Sequentially prints: 10, 12, 14 * } * ```*/staticinlinepublicfunctionto(from:Int, to:Float) returnnewIntIterator(from, to);
}
classFloatIterators {
/** * Float iterator from `from` (including) to `to` (excluding). * ``` * var value = 10.5; * for(i in value.floatTo(13)) { * trace(i); // Sequentially prints: 10.5, 11.5, 12.5 * } * for(i in value.floatTo(7)) { * trace(i); // Sequentially prints: 10.5, 9.5, 8.5 * } * for(i in value.floatTo(12).step(0.5)) { * trace(i); // Sequentially prints: 10.5, 11, 11.5 * } * ```*/staticinlinepublicfunctionfloatTo(from:Float, to:Float) returnnewFloatIterator(from, to);
}
classAnonymousObjectIterators {
/** * Object iterator which allows you to get field name & value in one go. * It is only guaranteed to work with anonymous objects. * * @throws iterators.exceptions.IllegalValueException - if `object` is not an object. * * ``` * var obj:Any = Json.parse(data); * for(f in obj.fields()) { * trace(f.name); * trace(f.value); * } * ```*/staticpublicinlinefunctionfields(object:Dynamic) returnnewFieldValueIterator(object);
}