HBCollections
HBCollections copied to clipboard
Categories for Foundation collection classes.
HBCollections
Objective-C categories for functional data structure traversal with blocks. The interface was inspired by Javascript Array Iteration Methods. The implementation was inspired by Mike Ash's Implementating Fast Enumeration Friday Q&A.
Submodules
This project uses git submodules Run immediately after cloning: git submodule update --init
Why
Your loop bodies and if predicates become reusable blocks, fine-grained code reuse points with which you can assemble larger systems.
Simple Examples
Instead of iterating your array with a for loop, you can enumerate your array. Instead of totalling your array with a for loop, you can reduce your array to a total.
Compatibility
Most methods return chainable NSEnumerators, so you can map, then add filtering, then add breaking, and still use a fast enumeration for loop (of course, you could always just reduce or enumerate it). (A more complex example).
Arrays, sets, and enumerators use the same API, [so your blocks can interoperate, unlike foundation block enumeration)][share-blocks].
Convenience
Although you can easily reduce an array to a dictionary, there is a convenience method to do it for you. Foundation APIs allow you to convert an enumerator to an array, but HBCollections gives you convenience methods to convert to a mutable array, set, and mutable set.
Implementation
There is a protocol in case you have your own data structure you want to traverse. Under the hood, everything uses fast enumeration and uses just one memory buffer for intermediate storage.