PHPackages                             voltra/lazy-collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. voltra/lazy-collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

voltra/lazy-collection
======================

A library for manipulating collections the lazy way

1.0.0(5y ago)028MITPHPPHP &gt;=7.1.11CI failing

Since Oct 10Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Voltra/lazy-collection)[ Packagist](https://packagist.org/packages/voltra/lazy-collection)[ RSS](/packages/voltra-lazy-collection/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (4)Versions (3)Used By (0)

[![lazy-collection logo](./res/lazy-collection.png)](./res/lazy-collection.png)

Lazy Collection
===============

[](#lazy-collection)

> A library for manipulating collections the lazy way

```
composer require voltra/lazy-collection
```

For more info, consult the [official documentation](https://voltra.github.io/lazy-collection) or join my [official discord server for help with libraries](https://discord.gg/JtWAjbw).

Using the library
-----------------

[](#using-the-library)

`use LazyCollection\Stream;`

There are two ways you can use this library :

- using the factory functions (in the `LazyCollection` namespace)
- using the factory static methods

### Functions

[](#functions)

There are a few functions predefined for you :

- `collect(...$args)` which creates a stream from the series of arguments
- `stream($iterable)` which creates a stream wrapping the given `iterable`
- `infiniteRange($start, $step)` which creates an infinite stream of numbers starting from `$start` incremented by `$step`
- `range($start = 0, $end = null, $step = 1)` which creates an kind of numbers range
- `splitBy($str, $separator, $removeEmptyStrings = true)` which creates a stream of strings by splitting `$str` into parts using `$separator`
- `splitByRegex($str, $re, $removeEmptyStrings = true)` which creates a stream of strings by splitting `$str` into parts using the regular expression `$re` (cf. \[preg\_split\](Make a stream by splitting a string in parts using a regular expression))

### Factories

[](#factories)

- `Stream::fromIterable($iterable)`
- `Stream::range($start = 0, $end = null, $step = 1)`
- `Stream::splitBy($str, $separator, $removeEmptyStrings = true`
- `splitByRegex($str, $re, $removeEmptyStrings = true)`

Extend the library
------------------

[](#extend-the-library)

`Stream` provides utilities to add methods and factories :

- `Stream::registerMethod($name, $method)` which can return an instance of `Stream` or something else
- `Stream::registerFactory($name, $factory)` which should return an instance of `Stream`

```
use LazyCollection\Stream;

Stream::registerMethod("mapTo42", static function(){
    /**
     * @var Stream $this
     */
    return $this->map(static function(){ return 42; });
});

Stream::fromIterable([1, 2, 3])
    ->mapTo42()
    ->toArray(); //-> [42, 42, 42]

Stream::registerFactory("answerToLife", function(){
    $gen = (static function(){
        yield 42;
    })();

    return new static($gen, false); // new static($generator, $isAssociative)

    /*
    Alternatively:

    return static:fromIterable([42]);
    */
});

Stream::answerToLife()->toArray(); //-> [42]
```

Why use this library
--------------------

[](#why-use-this-library)

Its goal is to provide a standalone library for collection manipulation with an elegant and fluent syntax and performance.

Because of its design, the following pieces are strictly equivalent in terms of complexity :

```
$items = [1, 2, 3, 4, 5, 6];
$results = [];

foreach($items as $item){
    $mapped = 3 * $item - 2; // models 3x-2
    if($mapped % 2 === 0)
        $results[] = $mapped;
}

$streamResults = Stream::fromIterable($items)
    ->map(function($x){ return 3 * $x - 2; })
    ->filter(function($x){ return $x % 2 === 0; })
    ->toArray();

// $results is the same as $streamResults
```

No matter how much operations you use, it will always be `O(n)`. The equivalent of a single for-loop.

Note that some operations like `reverse` or the likes of `unique` and `sort` are considered **eager** operations (or **stateful**) as they need to iterate over the entire stream once before emitting values themselves.

The point is, any operation you do cost, in the worst case scenario, as much as what you could write by hand.

Badges
------

[](#badges)

[![GitHub code size in bytes](https://camo.githubusercontent.com/4893098c6cdd03aaa89fded1182638a0389b036ead2cc264ee9caff01918fca3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f566f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/4893098c6cdd03aaa89fded1182638a0389b036ead2cc264ee9caff01918fca3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f566f6c7472612f6c617a792d636f6c6c656374696f6e) [![Packagist Downloads](https://camo.githubusercontent.com/c9af8babd47c0dff6326c50daee3e234166404dea2261a5289bd760042d42e0d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f766f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/c9af8babd47c0dff6326c50daee3e234166404dea2261a5289bd760042d42e0d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f766f6c7472612f6c617a792d636f6c6c656374696f6e) [![Packagist License](https://camo.githubusercontent.com/2f6212b5f3cebb8e4946f4758df1713caad9e37c1711febfda173eafbcf1aeb9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f766f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/2f6212b5f3cebb8e4946f4758df1713caad9e37c1711febfda173eafbcf1aeb9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f766f6c7472612f6c617a792d636f6c6c656374696f6e) [![GitHub issues](https://camo.githubusercontent.com/0fb408dd7906d635f03b1b08ea90f7fbf72d5bda3e54584686a804b80264c80b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f566f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/0fb408dd7906d635f03b1b08ea90f7fbf72d5bda3e54584686a804b80264c80b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f566f6c7472612f6c617a792d636f6c6c656374696f6e) [![GitHub pull requests](https://camo.githubusercontent.com/e1dcfc1bd495a613b70106210ce825fdd7511fbb5d25f24c26ccc97e4c1bdda7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722d7261772f566f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/e1dcfc1bd495a613b70106210ce825fdd7511fbb5d25f24c26ccc97e4c1bdda7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722d7261772f566f6c7472612f6c617a792d636f6c6c656374696f6e) [![Packagist Stars](https://camo.githubusercontent.com/fed8d0e5f4c0efc6ad3edfc42a825c4115df9e881aec68038e099ce701f3d7b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f766f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/fed8d0e5f4c0efc6ad3edfc42a825c4115df9e881aec68038e099ce701f3d7b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f766f6c7472612f6c617a792d636f6c6c656374696f6e) [![Packagist PHP Version Support](https://camo.githubusercontent.com/9bd546d187af28a532632f3a85f7471d19a6034270be4e5774291073d0909367/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f766f6c7472612f6c617a792d636f6c6c656374696f6e)](https://camo.githubusercontent.com/9bd546d187af28a532632f3a85f7471d19a6034270be4e5774291073d0909367/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f766f6c7472612f6c617a792d636f6c6c656374696f6e)

[![forthebadge](https://camo.githubusercontent.com/f51587462f5fc3f41b9c9987475de8988e21113d16baf3e420eee398b49ffb44/68747470733a2f2f666f7274686562616467652e636f6d2f696d616765732f6261646765732f6275696c742d776974682d6c6f76652e737667)](https://forthebadge.com)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2046d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/802602e0aeb4b70b8385bfa34308b3d1aa55baa9b41d9216cbd824da0d7bcad0?d=identicon)[Voltra](/maintainers/Voltra)

---

Top Contributors

[![Voltra](https://avatars.githubusercontent.com/u/25990549?v=4)](https://github.com/Voltra "Voltra (41 commits)")

---

Tags

collectionshacktoberfestlazy-evaluationphpphp-library

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/voltra-lazy-collection/health.svg)

```
[![Health](https://phpackages.com/badges/voltra-lazy-collection/health.svg)](https://phpackages.com/packages/voltra-lazy-collection)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
