Download the PHP package camille-hdl/lazy-lists without Composer
On this page you can find all versions of the php package camille-hdl/lazy-lists. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download camille-hdl/lazy-lists
More information about camille-hdl/lazy-lists
Files in camille-hdl/lazy-lists
Package lazy-lists
Short Description Lazy list processing and transducers
License MIT
Homepage https://github.com/camille-hdl/lazy-lists
Informations about the package lazy-lists
camille-hdl/lazy-lists
LazyLists is a toolbox for iterating over a Traversable
and transforming it. This is your typical map
, filter
, pipe
library, but with a twist: you only ever iterate at most once.
Using \LazyLists\pipe()
, you can compose filter
, map
, reduce
, each
, until
(...) into a single function that will iterate over your input only once (even less than once if you use take
or until
), thus, "Lazy".
For example, let's say we want to compute something about the 50 first products ordered in the Technology category of an online store:
Even if $orders
is very large, $getUsefulInsight
will only step through it until $isTechnologyRelated
has let 50 items through (or $orders
runs out before that), then stop the iteration early and return the final result of $computeInsight
.
$getProductsInOrder
and $isTechnologyRelated
will be called only as long as they are needed.
This is particularly useful if the cost of iteration is high.
Alternatively, you can just use the functions directly: $output = \LazyLists\map($transformation, $input)
.
You can use these features on arrays or Traversable
s such as iterators.
See the examples below.
Installation
The preferred method of installation is via Composer. Run the following
command to install the package and add it as a requirement to your project's
composer.json
:
Usage
You can use the functions directly on arrays or Traversable
s
but the most interesting way of using LazyLists is to use the composition functions : pipe
or iterate
.
Steps in the pipeline are executed sequentially for each element in the collection in a single iteration, unlike array_map
, array_filter
or other similar libraries.
Gotchas
- When iterating over arrays, keys are never kept, by design.
- The return value of
pipe(...)($input)
depends on the last function in the composition, according to the following heuristic, which is intuitive to me but maybe not to you:- if the last function is one that returns a single value, such as
reduce()
, the return value will be this value; - if the last function is one that transforms an input set into an output set (such as
map
orflatten
), the return value will be the output set (as a flat array); - if the last function is one that restricts the output set, such as
filter
,take
oruntil
, the return value will be the output set (as a flat array);
- if the last function is one that returns a single value, such as
iterate(...)($input)
behaves in the same way, but returns an iterator instead of an array.
Extending LazyLists
- You can create your own function usable with
pipe()
anditerate()
by having it return a\Lazy\Transducer\TransducerInterface
- You can create your own composition function (to use instead of
pipe()
oriterate()
). It should be relatively easy to implement something that can take a stream as input for example. Look-up the source code ofpipe()
(its very short!) and\Lazy\LazyWorker
(its less short!).
Performance considerations
LazyLists is optimized for minimizing the number of iterations while (hopefully) allowing for API elegance when transforming an input set.
Thus, the lower the cost of iteration, the less incentive there is to use this library.
If your program is only iterating over fast, in-memory data structures, performance will almost always be worse than using the built-in array_*
functions. That said, if you use \LazyLists\pipe()
to compose your functions, the performance gap reduces as the number of iterations increases.
You can see for yourself by running composer run phpbench
.
However, using \LazyLists\pipe()
will probably be beneficial in terms of performance if you either:
- use I/O during iteration, or
- use
\LazyLists\take()
and\LazyLists\until()
to restrict your output set.
Inspiration
This library attempts a subset of what transducers can do. There are transducer libraries out there, but I hope to bring a simpler API.
Contributing
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Contributions are welcome! Please read CONTRIBUTING for details.
Copyright and License
The camille-hdl/lazy-lists library is copyright © Camille Hodoul and licensed for use under the MIT License (MIT). Please see LICENSE for more information.