zalas/collections

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A tiny collection library

v1.1.0 2020-04-06 20:46 UTC

This package is auto-updated.

Last update: 2021-10-07 00:06:04 UTC


README

Build Status Scrutinizer Code Quality

A tiny collection library enabling array_* like operations on generators and other traversables.

Collection interface

interface Collection extends IteratorAggregate
{
    public function merge(Collection $other): Collection;

    public function filter(callable $f): Collection;

    public function map(callable $f): Collection;

    /**
     * @param mixed $initial
     * @param callable $f
     *
     * @return mixed
     */
    public function reduce($initial, callable $f);
}

Collection implementations

LazyCollection

The lazy collection implementation postpones evaluation of elements until it's necessary.

use Zalas\Collection\LazyCollection;

$c = (new LazyCollection(new \ArrayIterator([1, 2, 3, 4])))
    ->filter(fn (int $e) => 0 === $e % 2)
    ->map(fn (int $e) => $e * 2)
;

In legacy PHP versions:

use Zalas\Collection\LazyCollection;

$c = (new LazyCollection(new \ArrayIterator([1, 2, 3, 4)))
    ->filter(function (int $e) {
        return 0 === $e % 2;
    })
    ->map(function (int $e) {
        return $e * 2;
    })
;

Installation

composer require zalas/collections

Contributing

Please read the Contributing guide to learn about contributing to this project. Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.