robrix / RXCollections

Obsolete—use https://github.com/robrix/Reducers instead.
MIT License
475 stars 25 forks source link

Obsolete!

This is obsolete and unmaintained. Don’t use it! Use Reducers or ReactiveCocoa or Swift instead.

RXCollections

Lazily-evaluated (when feasible) higher-order functions for Cocoa collections (including your own), with as little chaff as possible.

In short:

#import <RXCollections/RXCollections.h>

…
RXMap(people, ^(Pal *each) { return each.phoneNumber; });

RXFilter(ungulates, ^(id<Ungulate> each) { return each.stomachCount == 4; });

RXFold(chunksOfText, @"", ^(NSString *full, NSString *each) { return [full stringByAppendingString:each]; });

RXLinearSearch(scientists, ^bool(Scientist *each, bool *stop) { return [each.name isEqualToString:@"Richard Feynman"]; });

RXConvolveWith(@[names, addresses, phoneNumbers], ^(NSUInteger count, id const objects[count]) {
    return [BusinessCard withName:objects[0] address:objects[1] phoneNumber:objects[2]];
});

(Yeah, yeah, you can call it RXZipWith instead if you like that so much better.)

Collections that can be created

Maps and filters return enumerations, which can be made into concrete collections with the RXConstruct… functions, e.g. RXConstructArray(RXMap(…)). It doesn’t matter what you’re mapping or filtering if you’re constructing a set or array; constructing dictionaries, however, requires the enumeration to produce RXKeyValuePair-conformant objects so that it can associate the keys and values it inserts.

Collections that can be traversed

RXMap, RXFold, RXFilter, RXLinearSearch can traverse anything conforming to NSFastEnumeration (which includes, among other things, NSEnumerator and NSManagedObjectModel). The returned object can itself be enumerated, as well as composed with other traversals safely and cleanly.

Notes

Future