Download the PHP package zicht/itertools without Composer
On this page you can find all versions of the php package zicht/itertools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zicht/itertools
More information about zicht/itertools
Files in zicht/itertools
Package itertools
Short Description A collection of functions that mirror the Python itertools library
License MIT
Informations about the package itertools
Zicht Iterator Tools Library
The Iterator Tools, or itertools for short, are a collection of convenience tools to handle sequences of data such as arrays, iterators, and strings. Some of the naming and API is based on the Python itertools.
Examples
Common operations include:
- mapping:
map
andmapBy
- filtering:
filter
,difference
- sorting:
sorted
- grouping:
groupBy
- reducing:
accumulate
,collapse
, andreduce
Usage
In order to use the available itertools filters/functions via Twig, simply add this service definition in your config/services.yaml
Scripts
- unit test:
composer test
- lint test:
composer lint
Example data
The examples below will use the following data to illustrate how various Iterator tools work:
Examples
With the example data above, this is how you could use itertools to get all unique colors of the cars in alphabetical order:
You can achieve the same in Twig:
Getter strategy
Many itertools can be passed a $strategy
parameter. This parameter
is used to obtain a value from the elements in the collection. The
$strategy
can be one of three things:
-
null, in which case the element itself is returned. For example:
Or in Twig:
-
a closure, in which case the closure is called with the element value and key as parameters to be used to compute a return value. For example:
Or in Twig:
-
a string, in which case this string is used to create a closure that tries to find public properties, methods, or array indexes. For example:
Or in Twig:
The string can consist of multiple dot separated words, allowing access to nested properties, methods, and array indexes.
If one of the words in the string can not be resolved into an existing property, method, or array index, the value
null
will be returned. For example:Or in Twig:
Fluent interface
One way to use the Iterator Tools is to convert the array, Iterator,
string, etc into an IterableIterator
. This class provides a fluent
interface to all of the common operations. For example:
Or in Twig:
Mapping
Mapping converts one collection into another collection of equal
length. Using map
allows manipulation of the elements while mapBy
allows manipulation of the collection keys.
For example, we can use a closure to create a title for each element
in $vehicles
:
Using the string getter strategy we can easily get
the types for each element in $vehicles
mapped by the vehicle
identifiers. For example:
Or in Twig:
There are several common mapping closures available
in mappings.php. Calling these
functions returns a closure that can be passed to map
and mapBy
.
For example:
Or in Twig:
Filtering
Filtering converts one collection into another, possibly shorter,
collection. Using filter
each element in the collection is
evaluated, the elements that are considered empty
will be rejected,
while the elements that are not empty
will be allowd to pass through
the filter.
For example, we can use a closure to determine if an element is
expensive, the filter
will then only allow the expensive elements
through:
Or in Twig:
Using the string getter strategy we can get only
the $vehicles
that are considered to be cool. For example:
Or in Twig:
There are several common filter closures available
in filters.php. Calling these
function returns a closure that can be passed to filter
. For
example:
Or in Twig:
Sorting
sorted
converts one collection into another collection of equal size
but with the elements possibly reordered.
For example, using the null
getter strategy,
which is the default, we will sort using the element values in
ascending order:
Or in Twig:
The sorting algorithm will preserve the keys and is guaranteed to be stable. I.e. when elements are sorted using the same value, then the sorted order is guaranteed to be the same as the order of the input elements. This is contrary to the standard PHP sorting functions.
Using the closure getter strategy the returned value is used to determine the order. The closure is called exactly once per element, and the resulting values must be comparable. For example:
The mappings.php provides a mapping closure which returns a random number. This can be used to sort a collection in a random order. For example:
Or in Twig:
Grouping
groupBy
converts one collection into one or more collections that
group the elements together on a specific criteria.
For example, using the string getter strategy we
can group all the $vehicles
of the same type together:
Or in Twig:
Not that the original keys of the vehicles are still part of the
resulting groups, and the elements within each group keep the order
that they had in the input, i.e. it uses the stable sorting provided
by sorted
.
Reducing
reduce
converts a collection into a single value by calling a
closure of two arguments cumulatively to the elements in the
collection, from left to right.
For example, without any arguments reduce
will add all elements of
the collection together:
Or in Twig:
In the above example, the default closure that is used looks like this:
Given that $numbers
consists of the elements {1, 3, 2, 5, 4}, the
add
closure is called four times:
There are several common reduction closures available
in reductions.php. Calling
these functions returns a closure that can be passed to reduction
.
For example:
Or in Twig:
Another common reduction is chaining multiple lists together into one list.
We call this process collapse. This process can also be achieved using reduce
and chain
together, however, because it is used frequently the collapse
helper
makes its usage easier, for example:
Or in Twig:
Maintainer(s)
- Boudewijn Schoon [email protected]
- Virginia Meijer [email protected]