Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Add mutable collection filtering and transforming.
Browse files Browse the repository at this point in the history
  • Loading branch information
zwaldowski committed Jul 7, 2011
1 parent 48d1af5 commit 10fe566
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 13 deletions.
2 changes: 1 addition & 1 deletion BlocksKit/NSDictionary+BlocksKit.h
Expand Up @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions 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. <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
46 changes: 46 additions & 0 deletions 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
44 changes: 44 additions & 0 deletions 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. <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
46 changes: 46 additions & 0 deletions 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
48 changes: 48 additions & 0 deletions 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. <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
47 changes: 47 additions & 0 deletions 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
28 changes: 16 additions & 12 deletions README.md
Expand Up @@ -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
* [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.

0 comments on commit 10fe566

Please sign in to comment.