Download the PHP package thecrypticace/lazy without Composer
On this page you can find all versions of the php package thecrypticace/lazy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thecrypticace/lazy
More information about thecrypticace/lazy
Files in thecrypticace/lazy
Package lazy
Short Description Collection-like wrapper around Iterators
License MIT
Informations about the package lazy
Lazy Collections
What
Lazy is a Collection-like wrapper that can operate on iterators one item at a time (on-demand).
Some notes:
- Lazy works well for one-shot data sources (e.g.
Generator
s) - Lazy supports algorithms which run with minimal overhead. Functional algorithms are well suited to this.
- Lazy collections can convert the underlying data source into an array using
->eager()
. Useful for reiterative purposes. - Has convenience methods for simple lazy data generation (e.g. ranges)
- Has higher-order collection proxy which will allow code like this:
$collection->map->people->map->count()->sum()
Why?
Let's say you have this code:
For a small array $items
the work performed here is trivial. If you take a large array containing 1 million numbers starting at one the work performed is large:
- 1,000,000 iterations in
filter
- 500,000 iterations in
map
- 500,000 iterations in
filter
- one iteration in
first
Total: 2,000,001 iterations Total: 2,000,001 function calls
Each one of these iterations also incurs the overhead of a function call.
If you replace that call to collect
with lazy
:
These are the stats:
- 400 iterations in
filter
- 200 iterations in
map
- 200 iterations in
filter
- one iteration in
first
Since these iterations happen on demand it's not 801 iterations. It's 400 for the entire set. Lazy Collections perform the minimal amount of work needed to get the result.
There are 801 function calls. One for each of the "virtual" iterations in each of the operations. Still, this number is far less than the 2,000,001 calls from the loop above.
Total: 400 iterations Total: 801 function calls
Lazy is effectively performing the same operation as this:
tip: lazy_range(1, 1000000)
will generate a Collection with a range from one to one million.