praswicaksono/lazy-collection

dev-master 2016-03-09 14:07 UTC

This package is not auto-updated.

Last update: 2024-12-13 19:02:46 UTC


README

Build Status

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