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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package itertools

Build Status Scrutinizer Code Quality Code Coverage

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:

Usage

In order to use the available itertools filters/functions via Twig, simply add this service definition in your config/services.yaml

Scripts

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:

  1. null, in which case the element itself is returned. For example:

    Or in Twig:

  2. 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:

  3. 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)


All versions of itertools with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zicht/itertools contains the following files

Loading the files please wait ....