This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mutable collection filtering and transforming.
- Loading branch information
1 parent
48d1af5
commit 10fe566
Showing
8 changed files
with
296 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. <https://github.com/MSch>. 2011. MIT. | ||
- Zach Waldowski. <https://github.com/zwaldowski>. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. <https://github.com/MSch>. 2011. MIT. | ||
- Zach Waldowski. <https://github.com/zwaldowski>. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. <https://github.com/MSch>. 2011. MIT. | ||
- Zach Waldowski. <https://github.com/zwaldowski>. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters