diff --git a/BlocksKit/NSDictionary+BlocksKit.h b/BlocksKit/NSDictionary+BlocksKit.h index d711cf32f..b4f0eb06e 100755 --- a/BlocksKit/NSDictionary+BlocksKit.h +++ b/BlocksKit/NSDictionary+BlocksKit.h @@ -27,7 +27,7 @@ */ - (void)each:(BKKeyValueBlock)block; -/** Loops through a dictionary to find the key/value pair matching the block. +/** Loops through a dictionary to find the key/value pairs matching the block. @param block A BOOL-returning code block for a key/value pair. @return Returns a dictionary of the objects found, `nil` otherwise. diff --git a/BlocksKit/NSMutableArray+BlocksKit.h b/BlocksKit/NSMutableArray+BlocksKit.h new file mode 100644 index 000000000..eea694099 --- /dev/null +++ b/BlocksKit/NSMutableArray+BlocksKit.h @@ -0,0 +1,48 @@ +// +// NSMutableArray+BlocksKit.h +// BlocksKit +// + +/** Block extensions for NSMutableArray. + + These utilities expound upon the BlocksKit additions + to the immutable superclass by allowing certain utilities + to work on an instance of the mutable class, saving memory + by not creating an immutable copy of the results. + + Includes code by the following: + + - Martin Schürrer. . 2011. MIT. + - Zach Waldowski. . 2011. MIT. + + @see NSArray(BlocksKit) + */ +@interface NSMutableArray (BlocksKit) + +/** Filters a mutable array to the objects matching the block. + + @param block A single-argument, BOOL-returning code block. + @see reject: + */ +- (void)select:(BKValidationBlock)block; + +/** Filters a mutable array to all objects but the ones matching the block, + the logical inverse to select:. + + @param block A single-argument, BOOL-returning code block. + @see select: + */ +- (void)reject:(BKValidationBlock)block; + +/** Transform the objects in the array to the results of the block. + + This is sometimes referred to as a transform, mutating one of each object: + [foo map:^id(id obj) { + return [dateTransformer dateFromString:obj]; + }]; + + @param block A single-argument, object-returning code block. + */ +- (void)map:(BKTransformBlock)block; + +@end \ No newline at end of file diff --git a/BlocksKit/NSMutableArray+BlocksKit.m b/BlocksKit/NSMutableArray+BlocksKit.m new file mode 100644 index 000000000..7f39e545c --- /dev/null +++ b/BlocksKit/NSMutableArray+BlocksKit.m @@ -0,0 +1,46 @@ +// +// NSMutableArray+BlocksKit.m +// BlocksKit +// + +#import "NSMutableArray+BlocksKit.h" + +@implementation NSMutableArray (BlocksKit) + +- (void)select:(BKValidationBlock)block { + NSIndexSet *list = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { + return block(obj); + }]; + + if (!list.count) + return; + + [self removeObjectsAtIndexes:list]; +} + +- (void)reject:(BKValidationBlock)block { + NSIndexSet *list = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { + return !block(obj); + }]; + + if (!list.count) + return; + + [self removeObjectsAtIndexes:list]; +} + +- (void)map:(BKTransformBlock)block { + [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + id value = block(obj); + + if (!value) + value = [NSNull null]; + + if ([value isEqual:obj]) + return; + + [self replaceObjectAtIndex:idx withObject:value]; + }]; +} + +@end diff --git a/BlocksKit/NSMutableDictionary+BlocksKit.h b/BlocksKit/NSMutableDictionary+BlocksKit.h new file mode 100644 index 000000000..3968f677d --- /dev/null +++ b/BlocksKit/NSMutableDictionary+BlocksKit.h @@ -0,0 +1,44 @@ +// +// NSMutableDictionary+BlocksKit.h +// BlocksKit +// + +/** Block extensions for NSMutableDictionary. + + These utilities expound upon the BlocksKit additions + to the immutable superclass by allowing certain utilities + to work on an instance of the mutable class, saving memory + by not creating an immutable copy of the results. + + Includes code by the following: + + - Martin Schürrer. . 2011. MIT. + - Zach Waldowski. . 2011. MIT. + + @see NSDictionary(BlocksKit) + */ +@interface NSMutableDictionary (BlocksKit) + +/** Filters a mutable dictionary to the key/value pairs matching the block. + + @param block A BOOL-returning code block for a key/value pair. + @see reject: + */ +- (void)select:(BKKeyValueValidationBlock)block; + +/** Filters a mutable dictionary to the key/value pairs not matching the block, + the logical inverse to select:. + + @param block A BOOL-returning code block for a key/value pair. + @see select: + */ +- (void)reject:(BKKeyValueValidationBlock)block; + +/** Transform each value of the dictionary to a new value, as returned by the + block. + + @param block A block that returns a new value for a given key/value pair. + */ +- (void)map:(BKKeyValueTransformBlock)block; + +@end \ No newline at end of file diff --git a/BlocksKit/NSMutableDictionary+BlocksKit.m b/BlocksKit/NSMutableDictionary+BlocksKit.m new file mode 100644 index 000000000..5cc9d4681 --- /dev/null +++ b/BlocksKit/NSMutableDictionary+BlocksKit.m @@ -0,0 +1,46 @@ +// +// NSMutableDictionary+BlocksKit.m +// BlocksKit +// + +#import "NSMutableDictionary+BlocksKit.h" + +@implementation NSMutableDictionary (BlocksKit) + +- (void)select:(BKKeyValueValidationBlock)block { + NSMutableArray *keys = [NSMutableArray arrayWithCapacity:self.count]; + + [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (!block(key, obj)) + [keys addObject:key]; + }]; + + [self removeObjectsForKeys:keys]; +} + +- (void)reject:(BKKeyValueValidationBlock)block { + NSMutableArray *keys = [NSMutableArray arrayWithCapacity:self.count]; + + [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (block(key, obj)) + [keys addObject:key]; + }]; + + [self removeObjectsForKeys:keys]; +} + +- (void)map:(BKKeyValueTransformBlock)block { + [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + id value = block(key, value); + + if (!value) + value = [NSNull null]; + + if ([value isEqual:obj]) + return; + + [self setObject:value forKey:key]; + }]; +} + +@end diff --git a/BlocksKit/NSMutableSet+BlocksKit.h b/BlocksKit/NSMutableSet+BlocksKit.h new file mode 100644 index 000000000..9a5060790 --- /dev/null +++ b/BlocksKit/NSMutableSet+BlocksKit.h @@ -0,0 +1,48 @@ +// +// NSMutableSet+BlocksKit.h +// BlocksKit +// + +/** Block extensions for NSMutableSet. + + These utilities expound upon the BlocksKit additions + to the immutable superclass by allowing certain utilities + to work on an instance of the mutable class, saving memory + by not creating an immutable copy of the results. + + Includes code by the following: + + - Martin Schürrer. . 2011. MIT. + - Zach Waldowski. . 2011. MIT. + + @see NSSet(BlocksKit) + */ +@interface NSMutableSet (BlocksKit) + +/** Filters a mutable set to the objects matching the block. + + @param block A single-argument, BOOL-returning code block. + @see reject: + */ +- (void)select:(BKValidationBlock)block; + +/** Filters a mutable set to all objects but the ones matching the block, + the logical inverse to select:. + + @param block A single-argument, BOOL-returning code block. + @see select: + */ +- (void)reject:(BKValidationBlock)block; + +/** Transform the objects in the set to the results of the block. + + This is sometimes referred to as a transform, mutating one of each object: + [controllers map:^id(id obj) { + return [obj view]; + }]; + + @param block A single-argument, object-returning code block. + */ +- (void)map:(BKTransformBlock)block; + +@end diff --git a/BlocksKit/NSMutableSet+BlocksKit.m b/BlocksKit/NSMutableSet+BlocksKit.m new file mode 100644 index 000000000..df9aa6cd9 --- /dev/null +++ b/BlocksKit/NSMutableSet+BlocksKit.m @@ -0,0 +1,47 @@ +// +// NSMutableSet+BlocksKit.m +// BlocksKit +// + +#import "NSMutableSet+BlocksKit.h" + +@implementation NSMutableSet (BlocksKit) + +- (void)select:(BKValidationBlock)block { + NSSet *list = [self objectsPassingTest:^BOOL(id obj, BOOL *stop) { + return block(obj); + }]; + + if (!list.count) + return; + + [self setSet:list]; +} + +- (void)reject:(BKValidationBlock)block { + NSSet *list = [self objectsPassingTest:^BOOL(id obj, BOOL *stop) { + return !block(obj); + }]; + + if (!list.count) + return; + + [self setSet:list]; +} + +- (void)map:(BKTransformBlock)block { + [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { + id value = block(obj); + + if (!value) + value = [NSNull null]; + + if ([value isEqual:obj]) + return; + + [self addObject:value]; + [self removeObject:obj]; + }]; +} + +@end \ No newline at end of file diff --git a/README.md b/README.md index 493b0e7a9..bc0dffe74 100755 --- a/README.md +++ b/README.md @@ -46,15 +46,19 @@ BlocksKit takes, repurposes, fiddles with, and groups together a variety of bloc The following people (in alphabetical order) have their code lovingly enshrined in BlocksKit: -* Michael Ash. @mikeash -* Jiva DeVoe. @jivadevote -* Corey Floyd. @coreyfloyd -* Landon Fuller, Plausible Labs. -* Mirko Kiefer. @mirkok -* Robin Lu. @robin -* Jake Marsh. @jakemarsh -* Andy Matuschak. @andymatuschak -* Aleks Nesterow. @nesterow -* Kevin O'Neill. @kevinoneill -* Jonathan Rentzch. @rentzch -* Peter Steinberger. @steipete \ No newline at end of file +* [Michael Ash](https://github.com/mikeash). +* [Jiva DeVoe](https://github.com/jivadevoe). +* [Corey Floyd](https://github.com/coreyfloyd). +* [Landon Fuller, Plausible Labs](http://plausiblelabs.com). +* [Mirko Kiefer](https://github.com/mirkok). +* [Robin Lu](https://github.com/robin). +* [Jake Marsh](https://github.com/jakemarsh). +* [Andy Matuschak](https://github.com/andymatuschak). +* [Aleks Nesterow](https://github.com/nesterow). +* [Kevin O'Neill](https://github.com/kevinoneill). +* [Jonathan Rentzch](https://github.com/rentzch). +* [Peter Steinberger](https://github.com/steipete). +* [Martin Schürrer](https://github.com/MSch). +* [Zach Waldowski](https://github.com/zwaldowski). + +Individual credits exist in the header files and, consequently, in the documentation. \ No newline at end of file