Lazy collection library for PHP.
Wrap your iterables in a thin layer of pure lazyness, so they can be lazy-evaluated while you're being lazy too. :D
Collections are everywhere, but you usually don't want to materialize them in each operation. That's where lazyness come in handy. This allows you to map and filter in zero initialization time, for later evaluation when the time is right.
- Convert anything into an iterable: arrays, iterators, functions, generators... you name it. [*]
- Map and filter with multiple functions on initialization, execute once on evaluation
- Handles infinite iterators
[*] planned
Lazzzy requires PHP 5.4 or higher for basic usage.
Just use composer to add to your project:
composer require xphere/lazzzy@dev
NOTE: Lazzzy is still in alpha, so don't rely on current methods. Just sayin'
Static function. Converts almost anything into an iterable.
Returns a Container
object wrapping the iterable.
use Lazzzy\Container;
$container = Container::from(range(0, 1000));
Honors \IteratorAggregate
interface so you can foreach
over Container
s.
Iterates over the container, applying all transformations. Returns the resulting array.
use Lazzzy\Container;
$expected = ['a' => 0, 'b' => 1, 'c' => 2];
$container = Container::from($expected);
$values = $container->toAssoc();
$this->assertEquals($expected, $values);
Like toAssoc
method, but discards keys.
use Lazzzy\Container;
$expected = [0, 1, 2];
$values = ['a' => 0, 'b' => 1, 'c' => 2];
$container = Container::from($expected);
$values = $container->toAssoc();
$this->assertEquals($expected, $values);
Iterates over the container, executing fn
on each iteration.
Returns nothing.
use Lazzzy\Container;
$echo = function ($item) { echo $item, ', '; };
$container = Container::from(range(0, 5));
$container->each($echo);
/// Outputs "0, 1, 2, 3, " and returns nothing
Calls fn
transformation on each iteration of [a]
.
use Lazzzy\Container;
$expected = [0, 2, 4, 6];
$double = function ($item) { return $item * 2; };
$container = Container::from(range(0, 3))
->map($double)
;
$actual = $container->toArray();
$this->assertSame($expected, $actual);
Filters elements for which fn
function returns truthy values.
use Lazzzy\Container;
$expected = [1, 3];
$odd = function ($item) { return $item % 2 === 1; };
$container = Container::from(range(0, 3))
->filter($odd)
;
$actual = $container->toArray();
$this->assertSame($expected, $actual);
Takes n
items from the iterator. n
must be an integer greater than zero.
use Lazzzy\Container;
$expected = [0, 1];
$container = Container::from(range(0, 3))
->take(2)
;
$actual = $container->toArray();
$this->assertSame($expected, $actual);
Takes items while the condition fn
is truthy.
use Lazzzy\Container;
$expected = [0, 1];
$notEqualsTwo = function ($item) { return $item !== 2; };
$container = Container::from(range(0, 3))
->takeWhile($notEqualsTwo)
;
$actual = $container->toArray();
$this->assertSame($expected, $actual);
More to come
- Container::skip(n)
- Container::skipUntil(fn)
- Container::fold(fn, x?)
- Container::foldr(fn, x?)
- Container::find(fn)
- Container::every(fn)
- Container::any(fn)
- Container::size()
Please contribute with the project on GitHub
Berny Cantos Contact: be@rny.cc GitHub: xPheRe Twitter: xPheRe
Lazzzy is licensed under the MIT License. See LICENSE file for full details.