This library implements iteration primitives like map() and filter()
using generators. To a large part this serves as a repository for small
examples of generator usage, but of course the functions are also practically
quite useful.
All functions in this library accept arbitrary iterables, i.e. arrays,
traversables, iterators and aggregates, which makes it quite different from
functions like array_map() (which only accept arrays) and the SPL iterators
(which usually only accept iterators, not even aggregates). The operations are
of course lazy.
Install
To install with composer:
composer require nikic/iter
Functionality
A small usage example for the map() and range() functions:
As the functionality is implemented using generators the resulting iterators
are by default not rewindable. This library implements additional functionality
to allow creating rewindable generators.
You can find documentation for this in iter.rewindable.php,
here is just a small usage example of the two main functions:
<?phpuse iter\func;
require'path/to/vendor/autoload.php';
/* Create a rewindable map function which can be used multiple times */$rewindableMap = iter\makeRewindable('iter\\map');
$res = $rewindableMap(func\operator('*', 3), [1, 2, 3]);
/* Do a rewindable call to map, just once */$res = iter\callRewindable('iter\\map', func\operator('*', 3), [1, 2, 3]);
The above functions are only useful for your own generators though, for the
iter generators rewindable variants are directly provided with an
iter\rewindable prefix:
nikic/iter
Iteration primitives using generators
This library implements iteration primitives like
map()
andfilter()
using generators. To a large part this serves as a repository for small examples of generator usage, but of course the functions are also practically quite useful.All functions in this library accept arbitrary iterables, i.e. arrays, traversables, iterators and aggregates, which makes it quite different from functions like
array_map()
(which only accept arrays) and the SPL iterators (which usually only accept iterators, not even aggregates). The operations are of course lazy.Install
To install with composer:
Functionality
A small usage example for the
map()
andrange()
functions:You can find documentation and usage examples for the individual functions in iter.php, here I only list the function signatures as an overview:
As the functionality is implemented using generators the resulting iterators are by default not rewindable. This library implements additional functionality to allow creating rewindable generators.
You can find documentation for this in iter.rewindable.php, here is just a small usage example of the two main functions:
The above functions are only useful for your own generators though, for the
iter
generators rewindable variants are directly provided with aniter\rewindable
prefix: