All Projects → loophp → iterators

loophp / iterators

Licence: MIT license
The missing PHP iterators.

Programming Languages

PHP
23972 projects - #3 most used programming language
Makefile
30231 projects
Dockerfile
14818 projects

Labels

Projects that are alternatives of or similar to iterators

FoldsCUDA.jl
Data-parallelism on CUDA using Transducers.jl and for loops (FLoops.jl)
Stars: ✭ 48 (+182.35%)
Mutual labels:  iterators
staticstep
Provides truly zero-cost alternatives to Iterator::step_by for both incrementing and decrementing any type that satisfies RangeBounds<T: Copy + Default + Step>.
Stars: ✭ 13 (-23.53%)
Mutual labels:  iterators
Iterators.jl
Common functional iterator patterns. DEPRECATED in favour of IterTools.jl
Stars: ✭ 59 (+247.06%)
Mutual labels:  iterators
IterTools.jl
Common functional iterator patterns
Stars: ✭ 124 (+629.41%)
Mutual labels:  iterators
streaming
Fast, safe and composable streaming abstractions.
Stars: ✭ 104 (+511.76%)
Mutual labels:  iterators
data-parallelism
juliafolds.github.io/data-parallelism/
Stars: ✭ 22 (+29.41%)
Mutual labels:  iterators
blIteratorAPI
A template library for creating custom iterators
Stars: ✭ 22 (+29.41%)
Mutual labels:  iterators

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage Mutation testing badge License Donate!

PHP Iterators

Description

The missing PHP iterators.

Features

  • CachingIteratorAggregate
  • ClosureIterator: ClosureIterator(callable $callable, array $arguments = [])
  • ClosureIteratorAggregate: ClosureIteratorAggregate(callable $callable, array $arguments = [])
  • ConcatIterableAggregate
  • IterableIterator: IterableIterator(iterable $iterable)
  • IterableIteratorAggregate: IterableIteratorAggregate(iterable $iterable)
  • MultipleIterableAggregate
  • PackIterableAggregate
  • PausableIteratorAggregate
  • RandomIterableAggregate
  • ResourceIteratorAggregate
  • SimpleCachingIteratorAggregate
  • StringIteratorAggregate
  • TypedIteratorAggregate
  • UnpackIterableAggregate

Installation

composer require loophp/iterators

Usage

CachingIteratorAggregate

Let you cache any iterator. You then get \Generators rewindable for free.

This implementation does not use internal state to keep track of the current position of the iterator. The underlying mechanism is based on SPL \CachingIterator.

The pros of using that iterator is performance. It's blazing fast, it cannot compare to any other stateful custom implementations.

This iterator will cache keys and values, of any type.

<?php

// Generator
$generator = static function (): \Generator {
    yield true => 'foo';
    yield false => 'bar';
    yield ['foo', 'bar'] => 'foobar';
};

$iterator = new CachingIteratorAggregate($generator());

foreach ($iterator as $key => $value); // This will work.
foreach ($iterator as $key => $value); // This will also work.

PackIterableAggregate

<?php

// Generator
$generator = static function (): \Generator {
    yield true => 'foo';
    yield false => 'bar';
    yield ['foo', 'bar'] => 'foobar';
};

$iterator = new PackIterableAggregate($generator());

foreach ($iterator as $value);
/*
$value will yield the following values:

- [true, 'foo']
- [false, 'bar']
- [['foo', 'bar'], 'foobar']
*/

UnpackIterableAggregate

<?php

// Generator
$generator = static function (): \Generator {
    yield [true, 'foo'];
    yield [false, 'bar'];
    yield [['foo', 'bar'], 'foobar'];
};

$iterator = new UnpackIterableAggregate($generator());

foreach ($iterator as $key => $value);
/*
$key and $value will yield the following values:

- true => 'foo'
- false => 'bar'
- ['foo', 'bar'] => 'foobar'
*/

ClosureIterator

<?php

$callable = static fn (int $from, int $to) => yield from range($from, $to);

$iterator = new ClosureIterator($callable(10, 20));

IterableIterator

<?php

$iterator = new IterableIterator(range(1, 10));

PausableIteratorAggregate

<?php

$inputIterator = new ArrayIterator(range('a', 'e'));
$iteratorAggregate = new PausableIteratorAggregate($inputIterator);

$i = 0;
foreach ($iteratorAggregate as $v) {
    var_dump($v) // Print: 'a', 'b', 'c'
    if (++$i === 2) {
        break;
    }
}

foreach ($iteratorAggregate->rest() as $v) {
    var_dump($v) // Print: 'd', 'e'
}

RandomIterableAggregate

In order to properly use this iterator, the user need to provide an extra parameter seed. By default, this parameter is set to zero and thus, the resulting iterator will be identical to the original one.

Random items are selected by choosing a random integer between zero and the value of seed. If that value is zero, then the iterator will yield else it will skip the value and start again with the next one.

The bigger the seed is, the bigger the entropy will be and the longer it will take to yield random items. It's then up to the user to choose an appropriate value. Usually a good value is twice the approximate amount of items the decorated iterator has.

If you're willing to iterate multiple times on this, use the CachingIteratorAggregate to cache the results.

This iterator works on keys and values, of any type.

<?php

$seed = random_int(0, 1000);
$inputIterator = new ArrayIterator(range('a', 'e'));
$iterator = new RandomIterableAggregate($inputIterator, $seed);

foreach ($iterator as $v) {
    var_dump($v);
}

$iterator = new CachingIteratorAggregate(
    (new RandomIterableAggregate($inputIterator, $seed))->getIterator()
);

foreach ($iterator as $v) {
    var_dump($v);
}
foreach ($iterator as $v) {
    var_dump($v);
}

Code quality, tests, benchmarks

Every time changes are introduced into the library, Github runs the tests.

The library has tests written with PHPUnit. Feel free to check them out in the tests directory.

Before each commit, some inspections are executed with GrumPHP; run composer grumphp to check manually.

The quality of the tests is tested with Infection a PHP Mutation testing framework - run composer infection to try it.

Static analyzers are also controlling the code. PHPStan and PSalm are enabled to their maximum level.

Contributing

Feel free to contribute by sending Github pull requests. I'm quite responsive :-)

If you can't contribute to the code, you can also sponsor me on Github.

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].