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 infiniteInt
counter iterable, starting fromstart
, steppingstep
each time. -
repeat(element)
: Creates an infinite iterable, that repeatselement
. -
empty()
: Creates an empty iterable. -
one(element)
: Creates an iterable, that yieldselement
once.
Contents of Extensions
:
-
asIterable(it)
: Castsit
to anIterable<T>
. Useful for unambiguously referring to the extension functions when working withArray<T>
s. -
asOnceIterable(it)
: Casts theit
iterator to anIterable<T>
that can only be used once. -
toArray(it)
: Collects the elements ofit
into anArray<T>
. -
toMap(it, keySel)
: Collects the elements ofit
into aMap<K, V>
, using the keys thatkeySel
selects from each element. -
toMapProj(it, keySel, valSel)
: Same astoMap
, but the values are also selected withvalSel
. -
join(it, sep)
: Joins the elements ofit
withsep
into aString
, usingStringBuf.add
. -
first(it)
: Retrieves the first element ofit
. -
last(it)
: Retrieves the last element ofit
. -
nth(it, n)
: Retrieves then
th element ofit
. -
all(it, pred)
: Checks, ifpred
is true for all elements ofit
. -
any(it, pred)
: Checks, ifpred
is true for any element ofit
. -
count(it)
: Counts the number of elements init
. -
find(it, pred)
: Finds the first element init
, wherepred
is true. -
map(it, f)
: Transforms each element ofit
usingf
. -
filter(it, pred)
: Filters the elements ofit
, only keeping the elements wherepred
returns true. -
filterMap(it, f)
: Filters and transforms the elements ofit
usingf
. Only those elements are kept, wheref
returnsSome(x)
. -
enumerate(it)
: Appends the index to each element ofit
(making itIndexed<T>
). -
zip(it1, it2)
: Pairs up each element ofit1
andit2
into aPair<T, U>
. -
chain(it1, it2)
: Chains the start ofit2
to the end ofit1
. -
skip(it, n)
: Skips the firstn
elements ofit
. -
skipWhile(it, pred)
: Skips the first elements ofit
, as long aspred
is true for them. -
take(it, n)
: Only keeps the firstn
elements ofit
. -
takeWhile(it, pred)
: Only keeps the first elements ofit
, as long aspred
is true for them. -
scanl(it, seed, acc)
: Performs a step-by-step reduction, tarting withseed
, and feeding the partial result and the next element ofit
intoacc
. Essentially a step-by-stepfoldl
. -
foldl(it, seed, acc)
: Performs a fold left operation, starting withseed
, feeding in the elements ofit
and 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 ofit
into 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
.