praswicaksono / lazy-collection
dev-master
2016-03-09 14:07 UTC
Requires
- php: ^7
- nikic/iter: ^1.3
Requires (Dev)
- phpunit/phpunit: ^5.2
- squizlabs/php_codesniffer: ^2.5
This package is not auto-updated.
Last update: 2024-12-13 19:02:46 UTC
README
This collection implement nikic/iter to provide lazy initialization and operation by using Generator
Installation
composer require praswicaksono/lazy-collection dev-master
Usage
Construct From Generator
$iterable = function () { for ($i = 1; $i <= 5; $i++) { yield $i; } }; $collection = IterableCollection::fromGenerator($iterable());
Construct From Array
$collection = IterableCollection::fromArray([1, 2, 3, 4, 5]);
Example Usage
$iterable = function () { for ($i = 1; $i <= 5; $i++) { yield $i; } }; $result = IterableCollection::fromGenerator($iterable()) ->map(function ($value) { return $value * 10; }) ->filter(function ($value) { return $value > 30; }) ->reduce(function ($acc, $value, $startValue) { return $acc + $value; }); // $result = 90
For more information, checkout the test
suite.
Contribute
PRs are welcome!
License
MIT