dutekvejin/iterators

0.4.0 2023-07-07 08:32 UTC

This package is not auto-updated.

Last update: 2024-10-25 14:40:26 UTC


README

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.