Shared publicly  - 
 
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:
include_once('/where/did/you/unpack/that/src/IterTools.php');

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\keys($input)
IterTools\values($input)
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.
2
5
Yaroslav Fedevych's profile photoBo Maryniuk's profile photo
3 comments
 
I didn't get what is "mapxy" unless I read the code. Each method has to be documented, otherwise it is really hard to use, if you don't actually read the library code.

Just suggestion.
 
Well I found some time and the project grew a readme and some inline documentations, TWIMC.
Add a comment...