Skip to content

dutekvejin/iterators

Repository files navigation

iterators

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require dutekvejin/iterators

Usage

Dutek\Iterator\ChunkIterator

Chunks an \Iterator into arrays with size elements.

use Dutek\Iterator\ChunkIterator;

$iterator = new \ArrayIterator([1, 2, 3, 4, 5]);
$size = 2;
$chunkIterator = new ChunkIterator($iterator, $size);

assert(iterator_to_array($chunkIterator) === [[1, 2], [3, 4], [5]]);

Dutek\Iterator\MapIterator

Applies the callback to the elements of the given \Iterator.

use Dutek\Iterator\MapIterator;

$iterator = new \ArrayIterator([1, 2, 3, 4, 5]);
$callback = function (int $item) {
    return $item ** 2;
};
$mapIterator = new MapIterator($iterator, $callback);

assert(iterator_to_array($mapIterator) === [1, 4, 9, 16, 25]);

Credits

License

Released under MIT License - see the License File for details.

Packages

No packages published

Languages