I was annoyed one day that PHP has grown so much needed types like Iterators, ArrayObjects etc., but hasn't taught its built-in array_* functions to work with them (as some bugreports state, they won't even try). So I have rolled my own at https://github.com/jafd/PHPIterTools.

I suck terribly at documenting my work, mostly due to laziness and due to the fact that I not at all enjoy describing for the umpteenth time how map(), filter() and reduce() work. However this time I got a grip on myself and did my good enough to cover the source with unit tests.

Key points:

then use
IterTools\map($callable, $input)
IterTools\filter($callable, $input)
IterTools\mapxy($callable, $input) // iterate over an array/iterator calling callable(key, value) (think Python's dict.iteritems())
IterTools\reduce($callable, $input, $maybe_initial)
IterTools\merge($input1, $input2, $input3, ...)

for fun and profit.

All of these functions, except reduce(), return iterators which in my humble opinion are somewhat more efficient. All of them accept anything implementing Iterator. The iterator returned always has toArray() method which returns you an array if that's what you ultimately want.

In most cases, the keys are preserved. merge() behaves with numeric and non-numeric keys just like array_merge would.

Maybe some +PHP Developers would find these useful.
Shared publiclyView activity