Download the PHP package spencerwi/lazylist without Composer
On this page you can find all versions of the php package spencerwi/lazylist. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lazylist
PHP-LazyList
A lazy-list (or generator, or fused-stream, or seq, or whatever) library for PHP.
Supports PHP >=7.4.
Usage
Creation
Lazy lists can be created from arrays
They can also be created from callables that take an int
index and return
either some value or else \Spencerwi\Lazy_list\LazyList::STOP_ITERATION
to
signal the end of the list.
map
You can map over a lazy list, and it's a "lazy" map, so it doesn't call your mapper function until you actually force evaluation of the list.
filter
You can apply a filter predicate to a lazy list, and it'll "lazily" apply the filter, meaning that your filter function doesn't get called until you actually force evaluation of the list.
Iteration
You can also iterate over a lazy list more traditionally; it's an iterator.
It even supports index => value iteration:
take($count)
You can also take just a certain number of elements from the beginning:
toArray()
You can more directly dump the whole list out to an array with toArray()
:
BE VERY CAREFUL WITH THIS, as you can wind up using a generator to create an infinite list (if your generator never returns null), which will cause an infinite loop if you call toArray() on it. Unfortunately, there's no way to know ahead of time that a generator is infinite, or else we'd have the list throw an exception on trying to toArray() an infinite LazyList.
reduce()
Alongside map
, you also have its friend reduce
, which takes an initial value,
and a function that works "pairwise" along the list calling a function you
provide, first on initial and the first element, then on the previous result
and the second element, then on that result and the third element, and so on.
If you try to "reduce" an empty list, you just get the "initial" value back. That's why it's there.
THIS OPERATION IS "EAGER", meaning that it evaluates the whole list. As with
toArray()
, that means that if you have an infinite LazyList, reduce()
will
cause an infinite loop.