https://github.com/mudge/iterator
An assortment of Iterators for PHP.
https://github.com/mudge/iterator
Last synced: 11 months ago
JSON representation
An assortment of Iterators for PHP.
- Host: GitHub
- URL: https://github.com/mudge/iterator
- Owner: mudge
- License: bsd-3-clause
- Created: 2014-07-16T07:49:15.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-07-16T09:43:32.000Z (almost 12 years ago)
- Last Synced: 2025-08-11T04:39:15.902Z (11 months ago)
- Language: PHP
- Homepage:
- Size: 145 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Iterator [](https://travis-ci.org/mudge/iterator)
An assortment of [Iterators](http://www.php.net/manual/en/class.iterator.php)
for PHP based on [Guzzle Iterator](https://github.com/guzzle/iterator).
## FlatMapIterator
```haskell
FlatMapIterator :: Traversable a -> (a -> RecursiveIterator b) -> RecursiveIterator b
```
Maps over values in a
[`Traversable`](http://php.net/manual/en/class.traversable.php), applying a
function that returns a
[`RecursiveIterator`](http://uk3.php.net/manual/en/class.recursiveiterator.php)
(e.g. a
[`RecursiveArrayIterator`](http://uk3.php.net/manual/en/class.recursivearrayiterator.php))
and returns a new `RecursiveIterator` of the new values.
This can then be used with a
[`RecursiveIteratorIterator`](http://uk3.php.net/manual/en/class.recursiveiteratoriterator.php)
to flatten all values like so:
```php
$iterator = new FlatMapIterator(new \ArrayIterator(array(1, 2)), function ($x) {
return new \RecursiveArrayIterator(array($x, $x * 2));
});
$recursiveIterator = new \RecursiveIteratorIterator($iterator);
$values = iterator_to_array($recursiveIterator, false));
// => array(1, 2, 2, 4);
```
## References
* [Guzzle Iterator](https://github.com/guzzle/iterator);
* [Hoa Iterator](https://github.com/hoaproject/Iterator);
* [How does RecursiveIteratorIterator work in
PHP?](http://stackoverflow.com/questions/12077177/how-does-recursiveiteratoriterator-work-in-php);
## License
Copyright © 2014 Paul Mucur.
Distributed under the MIT License.