Bite is a functional enumeration library built on top of NSFastEnumeration, providing functions like take:, filter:, and map:. While many Foundation collections already provide similar operations, Bite lets you do them without introducing large or mutable intermediates.
Concept based on the Bite Java library.
pod 'Bite', '~> 0.3'#import <Bite/Bite.h>BITEEnumerator *bite = BITE(myArray);
BITEEnumerator *bite2 = BITE(myArray, 2); // Never enumerate over more than two elements at a time.
for (id obj in BITE(myArray)) {
// Use directly in a for-in block.
}BITEEnumerator *bite = BITE_INTO(self, @selector(parentViewController));
for (id viewController in bite) {
if ([viewController isKindOfClass:[MyViewController class]]) {
return viewController;
}
}Recursive bites iterate until they reach a nil value.
BITEEnumerator *bite = [BITE(myArray) filter^BOOL(id obj) { return [obj size].width < 100; }];
BITEEnumerator *bite2 = [bite skip:5];
for (id obj in [bite2 take:5]) {
[self doSomething:obj];
}-
- (BITEEnumerator *)take:(NSUInteger)countStops iterating aftercountelements are iterated. -
- (BITEEnumerator *)skip:(NSUInteger)countSkips overcountelements. -
- (BITEEnumerator *)map:(id (^)(id obj))mappingFunctionHas NSExpression versions. Passes each element through a mapping function and iterates the results. -
- (BITEEnumerator *)mapWithKeyPath:(NSString *)keyPathSame asmap:but selects a key path from each element. -
- (BITEEnumerator *)filter:(BOOL (^)(id obj))testHas NSPredicate versions. Only iterates elements that pass a test. Useful fornilfiltering before generating a collection. -
- (BITEEnumerator *)choke:(NSUInteger)chokeDecreases the number of elements that are loaded into the fast-enumeration buffer. -
- (BITEEnumerator *)except:(id)objFilters out elements that are equal to the given object. -
- (BITEEnumerator *)until:(BOOL (^)(id obj))testHas NSPredicate versions. Stops iterating when the test passes. -
- (BITEEnumerator *)and:(id<NSFastEnumeration>)enumeratorConcatenates this enumerator with another. -
- (BITEEnumerator *)andObject:(id)objConcatenates this enumerator with a single element.
-
- (NSUInteger)countReturns the number of elements in the enumerator. -
- (NSArray *)arrayCreates an array. -
- (NSSet *)setCreates a set. -
- (NSDictionary *)dictionaryWithKeyPath:(NSString *)keyPath valuePath:(NSString *)valuePathCreates a dictionary using key paths for the keys and values. -
- (NSDictionary *)dictionaryWithPairsCreates a dictionary using the path_1for the key and_2for the value. To be used withPAIR/BITE_TUPLE. -
- (NSString *)joinedByString:(NSString *)separatorJoins the elements in the enumerator with a separator. -
- (BOOL)any:(BOOL (^)(id obj))testHas NSPredicate versions. ReturnsYESif any item in the enumerator passes the test and stops iterating. It chokes the buffer to one element to limit execution. -
- (BOOL)all:(BOOL (^)(id obj))testHas NSPredicate versions. ReturnsYESif all items in the enumerator pass the test. ReturnsNOand stops iterating as soon as the test fails. It chokes the buffer to one element to limit execution. -
- (id)first:(out BOOL *)existsReturns the first item ornilif the enumerator is empty. Optionally outputs whether or not an item was returned toexistsin cases wherenilis a valid enumerated item. -
- (id)last:(out BOOL *)existsReturns the last item ornilif the enumerator is empty. Optionally outputs whether or not an item was returned toexistsin cases wherenilis a valid enumerated item. -
- (id)foldLeft:(id)initial func:(id (^)(id acc, id obj))funcFolds left with an initial accumulator value. -
- (id)foldRight:(id)initial func:(id (^)(id obj, id acc))funcFolds right with an initial accumulator value. -
- (id)reduceLeft:(id (^)(id acc, id obj))funcReduces left. -
- (id)reduceRight:(id (^)(id obj, id acc))funcReduces right.
BITETuple contains indexed properties _1, _2, _3 which can be created with the overloaded BITE_TUPLE function. Shorthand macros exist if you define BITE_SHORTHAND before importing bite:
id a = BITE_TUPLE(@1);
id b = BITE_TUPLE(@1, @2);
id c = BITE_TUPLE(@1, @2, @3);
// or
id d = MONAD(@1);
id e = PAIR(@1, @2);
id f = TREBLE(@1, @2, @3);
NSParameterAssert([e _2] == [f _2]);-
Creating a dictionary from an object using blocks and tuples.
NSDictionary *networkUsersForSites = [[[BITE(networkUsers) filter:^BOOL(StacManNetworkUser *user) { return user.siteUrl != nil; }] map:^id(StacManNetworkUser *user) { return PAIR(user.siteUrl, user); }] dictionaryWithPairs];
-
Creating the same with predicates and key paths:
NSDictionary *networkUsersForSites = [[BITE(networkUsers) filterWithFormat:@"siteUrl != nil"] dictionaryWithKeyPath:@"siteUrl" valuePath:@"self"];
-
Finding the parent table view for a cell.
UITableView *tableView = [[BITE_INTO(cell, @selector(superview)) filterWithFormat:@"self isKindOfClass: %@", [UITableView class]] first:NULL];

