Skip to content

bolknote/itertools

Repository files navigation

itertools

A PHP port of Python's itertools, built on generators.

Requires PHP 8.4+. All itertools helpers are implemented, plus iter, enumerate, and range.

Install

composer require bolknote/itertools

Usage

use Itertools\Itertools as it;

foreach (it::islice(it::cycle('ABC'), 10) as $element) {
    echo $element; // ABCABCABCA
}

Strings are treated as sequences of characters (like Python), alongside arrays and any Traversable.

Highlights

Area Methods
Infinite / slicing count, cycle, repeat, islice
Terminating accumulate, batched, chain, chain_from_iterable, compress, dropwhile, filterfalse, groupby, ifilter, imap, izip, izip_longest, pairwise, starmap, takewhile, tee
Combinatoric product, permutations, combinations, combinations_with_replacement
Extras iter, enumerate, range

Notes:

  • izip_longest(...$iterables, $fillvalue) — fill value is the last argument (PHP has no keyword-only params after a variadic).
  • product(...$iterables, $repeat) — if the last argument is an int, it is Python's repeat.
  • groupby yields [key, list] pairs (groups are materialized arrays).
  • tee shares a buffer so clones can be consumed independently / interleaved.
  • accumulate(..., $initial = null) — like Python, null means “no initial value”.

Development

composer update
composer test      # PHPUnit
composer phpstan   # static analysis

About

PHP generator Itertools

Resources

Stars

11 stars

Watchers

2 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages