Itertools
The Haxelib package can be found here.
Many languages have a fluent API that helps working with sequences and sequence manipulation: C# has LINQ, Java has Stream, Rust has std::iter::Iterator, C++ has Ranges and so on.
These APIs allow us to express complex transformations in a compact and declarative manner. Haxe has the Lambda class, which has a couple of problems:
-
It is eagerly evaluated, the results are usually immediately collected into an
Array<T>. This can be both wasteful, and constrasining, as it does not allow for potentially infinite sequences. -
It lacks quite a few operations: There's no skipping, zipping, reversing, checking if a predicate is true for all/any elements, ...
-
The API is not fluent, as it works on
Iterable<T>s instead ofIterator<T>s.
This library attempts to fill this hole in the Haxe ecosystem.
Examples
The full API documentation can be found here.
Collecting the names of students into a comma-separated string, who have have an average over 4:
var names = students
.filter(s -> s.average() > 4)
.map(s -> s.name)
.join(", ");
Maturity
Please note, that the library is still early-stage. If you feel like something fundamental/useful is missing, please open an issue!
Provided functionality
The full API documentation can be found here.
The library can be used through just 2 types: Basic and Extensions.
The Basic class contains functions that construct elemental iterators, like
a basic counter, or repeating an element infinitely. The Extensions class contains
extension functions for iterators, thus it is advised to use it with using itertools.Extensions;.
All operations (unless they reduce to a final result, like all or count) are lazy, meaning that they only perform computations, when they are iterated.
Contents of Basic:
-
count(start = 0, step = 1): Creates an infiniteIntcounter iterable, starting fromstart, steppingstepeach time. -
repeat(element): Creates an infinite iterable, that repeatselement. -
empty(): Creates an empty iterable. -
one(element): Creates an iterable, that yieldselementonce.
Contents of Extensions:
-
asIterable(it): Castsitto anIterable<T>. Useful for unambiguously referring to the extension functions when working withArray<T>s. -
asOnceIterable(it): Casts theititerator to anIterable<T>that can only be used once. -
toArray(it): Collects the elements ofitinto anArray<T>. -
toMap(it, keySel): Collects the elements ofitinto aMap<K, V>, using the keys thatkeySelselects from each element. -
toMapProj(it, keySel, valSel): Same astoMap, but the values are also selected withvalSel. -
join(it, sep): Joins the elements ofitwithsepinto aString, usingStringBuf.add. -
first(it): Retrieves the first element ofit. -
last(it): Retrieves the last element ofit. -
nth(it, n): Retrieves thenth element ofit. -
all(it, pred): Checks, ifpredis true for all elements ofit. -
any(it, pred): Checks, ifpredis true for any element ofit. -
count(it): Counts the number of elements init. -
find(it, pred): Finds the first element init, wherepredis true. -
map(it, f): Transforms each element ofitusingf. -
filter(it, pred): Filters the elements ofit, only keeping the elements wherepredreturns true. -
filterMap(it, f): Filters and transforms the elements ofitusingf. Only those elements are kept, wherefreturnsSome(x). -
enumerate(it): Appends the index to each element ofit(making itIndexed<T>). -
zip(it1, it2): Pairs up each element ofit1andit2into aPair<T, U>. -
chain(it1, it2): Chains the start ofit2to the end ofit1. -
skip(it, n): Skips the firstnelements ofit. -
skipWhile(it, pred): Skips the first elements ofit, as long aspredis true for them. -
take(it, n): Only keeps the firstnelements ofit. -
takeWhile(it, pred): Only keeps the first elements ofit, as long aspredis true for them. -
scanl(it, seed, acc): Performs a step-by-step reduction, tarting withseed, and feeding the partial result and the next element ofitintoacc. Essentially a step-by-stepfoldl. -
foldl(it, seed, acc): Performs a fold left operation, starting withseed, feeding in the elements ofitand the partial result intoacc. -
flatten(it): Flattens an iterable of iterables into a flat element structure (iterable<iterable<T>> -> iterable<T>). -
flatMap(it, f): Transforms each element ofitinto a sequence, that are then flattened. -
reverse(it): Reverses the order of the elements ofit. -
groupBy(it, keySel:T): Groups elements ofit, selecting the group key withkeySel.