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
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]]);
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]);
Released under MIT License - see the License File for details.