Skip to content

Commit

Permalink
typedef blocks for easy declaration and reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
mdippery committed Oct 21, 2013
1 parent 4e2b3fb commit ddb29a8
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 107 deletions.
4 changes: 4 additions & 0 deletions Collections Mac.xcodeproj/project.pbxproj
Expand Up @@ -24,6 +24,7 @@
B7A1C1F812E3FFE300CADC97 /* NSSet+Collections.h in Headers */ = {isa = PBXBuildFile; fileRef = B7A1C1F612E3FFE300CADC97 /* NSSet+Collections.h */; settings = {ATTRIBUTES = (Public, ); }; };
B7A1C1F912E3FFE300CADC97 /* NSSet+Collections.m in Sources */ = {isa = PBXBuildFile; fileRef = B7A1C1F712E3FFE300CADC97 /* NSSet+Collections.m */; };
B7A1C2CD12E407ED00CADC97 /* TestSetCollections.m in Sources */ = {isa = PBXBuildFile; fileRef = B7A1C2CC12E407ED00CADC97 /* TestSetCollections.m */; };
B7ABE4A21815EAAD00862E71 /* Collections.h in Headers */ = {isa = PBXBuildFile; fileRef = B7ABE4A11815EAA300862E71 /* Collections.h */; settings = {ATTRIBUTES = (Public, ); }; };
B7FAD22715C0729C00F9BA64 /* NSString+Collections.h in Headers */ = {isa = PBXBuildFile; fileRef = B7FAD22515C0729C00F9BA64 /* NSString+Collections.h */; };
B7FAD22815C0729C00F9BA64 /* NSString+Collections.m in Sources */ = {isa = PBXBuildFile; fileRef = B7FAD22615C0729C00F9BA64 /* NSString+Collections.m */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -61,6 +62,7 @@
B7A1C1F712E3FFE300CADC97 /* NSSet+Collections.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSSet+Collections.m"; path = "Source/NSSet+Collections.m"; sourceTree = "<group>"; };
B7A1C2CB12E407ED00CADC97 /* TestSetCollections.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestSetCollections.h; path = Tests/TestSetCollections.h; sourceTree = "<group>"; };
B7A1C2CC12E407ED00CADC97 /* TestSetCollections.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestSetCollections.m; path = Tests/TestSetCollections.m; sourceTree = "<group>"; };
B7ABE4A11815EAA300862E71 /* Collections.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Collections.h; path = Source/Collections.h; sourceTree = "<group>"; };
B7FAD22515C0729C00F9BA64 /* NSString+Collections.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSString+Collections.h"; path = "Source/NSString+Collections.h"; sourceTree = "<group>"; };
B7FAD22615C0729C00F9BA64 /* NSString+Collections.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSString+Collections.m"; path = "Source/NSString+Collections.m"; sourceTree = "<group>"; };
D2AAC0C705546C1D00DB518D /* libCollections.A.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libCollections.A.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -110,6 +112,7 @@
08FB77AEFE84172EC02AAC07 /* Library */ = {
isa = PBXGroup;
children = (
B7ABE4A11815EAA300862E71 /* Collections.h */,
B77EEBEE12E3907B00567D8C /* Common.h */,
B77EEBEF12E3907B00567D8C /* Common.m */,
B7BC512112EF845E00DD7603 /* Additions */,
Expand Down Expand Up @@ -191,6 +194,7 @@
files = (
B76C5D0A12DE0F250058DF08 /* NSArray+Collections.h in Headers */,
B77EEBF012E3907B00567D8C /* Common.h in Headers */,
B7ABE4A21815EAAD00862E71 /* Collections.h in Headers */,
B7A1C18B12E3FA0100CADC97 /* MDPair.h in Headers */,
B7A1C1F812E3FFE300CADC97 /* NSSet+Collections.h in Headers */,
B749BEA912F7412A00298360 /* CommonHelpers.h in Headers */,
Expand Down
33 changes: 33 additions & 0 deletions Source/Collections.h
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2013 Michael Dippery <michael@monkey-robot.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

@class NSString;


typedef id (^MDElementDefault)(void);
typedef BOOL (^MDElementFilter)(id);
typedef id (^MDElementInjector)(id, id);
typedef void (^MDElementMutator)(id);
typedef id (^MDElementTransformer)(id);

typedef void (^MDCharacterMutator)(unichar);
typedef void (^MDStringMutator)(NSString *);
29 changes: 15 additions & 14 deletions Source/Common.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Michael Dippery <michael@monkey-robot.com>
* Copyright (C) 2011-2013 Michael Dippery <michael@monkey-robot.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,6 +21,7 @@
*/

#import <Foundation/Foundation.h>
#import "Collections.h"

@class MDPair;

Expand All @@ -31,17 +32,17 @@

extern id do_comparison(id collection, NSComparator cmp, NSComparisonResult val);

extern void do_foreach(id collection, void (^block)(id));
extern id collect_foreach(id collection, id acc, id (^block)(id));
extern id detect_foreach(id collection, BOOL (^detect)(id), id (^none)(void));
extern id inject_foreach(id collection, id initial, id (^into)(id, id));
extern id select_foreach(id collection, id acc, BOOL (^block)(id));
extern id reject_foreach(id collection, id acc, BOOL (^block)(id));
extern void do_foreach(id collection, MDElementMutator);
extern id collect_foreach(id collection, id acc, MDElementTransformer);
extern id detect_foreach(id collection, MDElementFilter, MDElementDefault);
extern id inject_foreach(id collection, id initial, MDElementInjector);
extern id select_foreach(id collection, id acc, MDElementFilter);
extern id reject_foreach(id collection, id acc, MDElementFilter);

extern BOOL all_foreach(id collection, BOOL (^block)(id));
extern BOOL any_foreach(id collection, BOOL (^block)(id));
extern BOOL none_foreach(id collection, BOOL (^block)(id));
extern BOOL one_foreach(id collection, BOOL (^block)(id));
extern id drop_foreach(id collection, id acc, BOOL (^drop)(id));
extern MDPair *partition_foreach(id collection, id trueAcc, id falseAcc, BOOL (^block)(id));
extern id take_foreach(id collection, id acc, BOOL (^take)(id));
extern BOOL all_foreach(id collection, MDElementFilter);
extern BOOL any_foreach(id collection, MDElementFilter);
extern BOOL none_foreach(id collection, MDElementFilter);
extern BOOL one_foreach(id collection, MDElementFilter);
extern id drop_foreach(id collection, id acc, MDElementFilter);
extern MDPair *partition_foreach(id collection, id trueAcc, id falseAcc, MDElementFilter);
extern id take_foreach(id collection, id acc, MDElementFilter);
26 changes: 13 additions & 13 deletions Source/Common.m
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Michael Dippery <michael@monkey-robot.com>
* Copyright (C) 2011-2013 Michael Dippery <michael@monkey-robot.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,15 +24,15 @@
#import "MDPair.h"
#import "CommonHelpers.h"

void do_foreach(id collection, void (^block)(id))
void do_foreach(id collection, MDElementMutator block)
{
for (id val in collection) {
id item = [collection objectForValue:val];
block(item);
}
}

id collect_foreach(id collection, id acc, id (^block)(id))
id collect_foreach(id collection, id acc, MDElementTransformer block)
{
for (id val in collection) {
id item = [collection objectForValue:val];
Expand All @@ -41,15 +41,15 @@ id collect_foreach(id collection, id acc, id (^block)(id))
return [acc freeze];
}

id detect_foreach(id collection, BOOL (^detect)(id), id (^none)(void))
id detect_foreach(id collection, MDElementFilter detect, MDElementDefault none)
{
for (id item in collection) {
if (detect(item)) return item;
}
return none();
}

id inject_foreach(id collection, id initial, id (^into)(id, id))
id inject_foreach(id collection, id initial, MDElementInjector into)
{
for (id val in collection) {
id item = [collection objectForValue:val];
Expand All @@ -58,7 +58,7 @@ id inject_foreach(id collection, id initial, id (^into)(id, id))
return initial;
}

id select_foreach(id collection, id acc, BOOL (^block)(id))
id select_foreach(id collection, id acc, MDElementFilter block)
{
for (id val in collection) {
id item = [collection objectForValue:val];
Expand All @@ -69,7 +69,7 @@ id select_foreach(id collection, id acc, BOOL (^block)(id))
return [acc freeze];
}

id reject_foreach(id collection, id acc, BOOL (^block)(id))
id reject_foreach(id collection, id acc, MDElementFilter block)
{
for (id val in collection) {
id item = [collection objectForValue:val];
Expand All @@ -95,15 +95,15 @@ id do_comparison(id collection, NSComparator cmp, NSComparisonResult val)
return result;
}

BOOL all_foreach(id collection, BOOL (^block)(id))
BOOL all_foreach(id collection, MDElementFilter block)
{
for (id item in collection) {
if (!block(item)) return NO;
}
return YES;
}

BOOL any_foreach(id collection, BOOL (^block)(id))
BOOL any_foreach(id collection, MDElementFilter block)
{
for (id item in collection) {
if (block(item)) return YES;
Expand All @@ -119,7 +119,7 @@ BOOL none_foreach(id collection, BOOL (^block)(id))
return YES;
}

BOOL one_foreach(id collection, BOOL (^block)(id))
BOOL one_foreach(id collection, MDElementFilter block)
{
BOOL sawOne = NO;
for (id item in collection) {
Expand All @@ -131,7 +131,7 @@ BOOL one_foreach(id collection, BOOL (^block)(id))
return sawOne;
}

id drop_foreach(id collection, id acc, BOOL (^drop)(id obj))
id drop_foreach(id collection, id acc, MDElementFilter drop)
{
BOOL dropFailed = NO;
for (id val in collection) {
Expand All @@ -148,7 +148,7 @@ id drop_foreach(id collection, id acc, BOOL (^drop)(id obj))
return [acc freeze];
}

MDPair *partition_foreach(id collection, id trueAcc, id falseAcc, BOOL (^block)(id))
MDPair *partition_foreach(id collection, id trueAcc, id falseAcc, MDElementFilter block)
{
for (id val in collection) {
id item = [collection objectForValue:val];
Expand All @@ -158,7 +158,7 @@ id drop_foreach(id collection, id acc, BOOL (^drop)(id obj))
return [MDPair pairWithFirstObject:[trueAcc freeze] secondObject:[falseAcc freeze]];
}

id take_foreach(id collection, id acc, BOOL (^take)(id))
id take_foreach(id collection, id acc, MDElementFilter take)
{
for (id val in collection) {
id item = [collection objectForValue:val];
Expand Down
31 changes: 16 additions & 15 deletions Source/NSArray+Collections.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Michael Dippery <michael@monkey-robot.com>
* Copyright (C) 2011-2013 Michael Dippery <michael@monkey-robot.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,30 +21,31 @@
*/

#import <Foundation/Foundation.h>
#import "Collections.h"

@class MDPair;


@interface NSArray (SmalltalkCollections)
- (NSArray *)do:(void (^)(id obj))block;
- (NSArray *)collect:(id (^)(id obj))block;
- (id)detect:(BOOL (^)(id obj))block;
- (id)detect:(BOOL (^)(id obj))block ifNone:(id (^)(void))none;
- (id)inject:(id)initial into:(id (^)(id memo, id obj))block;
- (NSArray *)reject:(BOOL (^)(id obj))block;
- (NSArray *)select:(BOOL (^)(id obj))block;
- (NSArray *)do:(MDElementMutator)block;
- (NSArray *)collect:(MDElementTransformer)block;
- (id)detect:(MDElementFilter)block;
- (id)detect:(MDElementFilter)block ifNone:(MDElementDefault)none;
- (id)inject:(id)initial into:(MDElementInjector)block;
- (NSArray *)reject:(MDElementFilter)block;
- (NSArray *)select:(MDElementFilter)block;
@end

@interface NSArray (RubyEnumerable)
- (BOOL)all:(BOOL (^)(id obj))block;
- (BOOL)any:(BOOL (^)(id obj))block;
- (BOOL)none:(BOOL (^)(id obj))block;
- (BOOL)one:(BOOL (^)(id obj))block;
- (BOOL)all:(MDElementFilter)block;
- (BOOL)any:(MDElementFilter)block;
- (BOOL)none:(MDElementFilter)block;
- (BOOL)one:(MDElementFilter)block;
- (NSArray *)drop:(NSUInteger)n;
- (NSArray *)dropWhile:(BOOL (^)(id obj))block;
- (NSArray *)dropWhile:(MDElementFilter)block;
- (id)max:(NSComparator)block;
- (id)min:(NSComparator)block;
- (MDPair *)partition:(BOOL (^)(id obj))block;
- (MDPair *)partition:(MDElementFilter)block;
- (NSArray *)take:(NSUInteger)n;
- (NSArray *)takeWhile:(BOOL (^)(id obj))block;
- (NSArray *)takeWhile:(MDElementFilter)block;
@end
30 changes: 15 additions & 15 deletions Source/NSArray+Collections.m
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Michael Dippery <michael@monkey-robot.com>
* Copyright (C) 2011-2013 Michael Dippery <michael@monkey-robot.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -28,7 +28,7 @@

@implementation NSArray (SmalltalkCollections)

- (NSArray *)do:(void (^)(id obj))block
- (NSArray *)do:(MDElementMutator)block
{
if ([self respondsToSelector:@selector(enumerateObjectsUsingBlock:)]) {
[self enumerateObjectsUsingBlock:^ (id obj, NSUInteger idx, BOOL *stop) { block(obj); }];
Expand All @@ -38,34 +38,34 @@ - (NSArray *)do:(void (^)(id obj))block
return self;
}

- (NSArray *)collect:(id (^)(id obj))block
- (NSArray *)collect:(MDElementTransformer)block
{
NSMutableArray *a = [NSMutableArray arrayWithCapacity:[self count]];
return collect_foreach(self, a, block);
}

- (id)detect:(BOOL (^)(id obj))block
- (id)detect:(MDElementFilter)block
{
return default_detect(self, block);
}

- (id)detect:(BOOL (^)(id obj))block ifNone:(id (^)(void))none
- (id)detect:(MDElementFilter)block ifNone:(MDElementDefault)none
{
return detect_foreach(self, block, none);
}

- (id)inject:(id)initial into:(id (^)(id memo, id obj))block
- (id)inject:(id)initial into:(MDElementInjector)block
{
return inject_foreach(self, initial, block);
}

- (NSArray *)reject:(BOOL (^)(id obj))block
- (NSArray *)reject:(MDElementFilter)block
{
NSMutableArray *a = [NSMutableArray arrayWithCapacity:[self count]];
return reject_foreach(self, a, block);
}

- (NSArray *)select:(BOOL (^)(id obj))block
- (NSArray *)select:(MDElementFilter)block
{
NSMutableArray *a = [NSMutableArray arrayWithCapacity:[self count]];
return select_foreach(self, a, block);
Expand All @@ -75,22 +75,22 @@ - (NSArray *)select:(BOOL (^)(id obj))block

@implementation NSArray (RubyEnumerable)

- (BOOL)all:(BOOL (^)(id obj))block
- (BOOL)all:(MDElementFilter)block
{
return all_foreach(self, block);
}

- (BOOL)any:(BOOL (^)(id obj))block
- (BOOL)any:(MDElementFilter)block
{
return any_foreach(self, block);
}

- (BOOL)none:(BOOL (^)(id obj))block
- (BOOL)none:(MDElementFilter)block
{
return none_foreach(self, block);
}

- (BOOL)one:(BOOL (^)(id obj))block
- (BOOL)one:(MDElementFilter)block
{
return one_foreach(self, block);
}
Expand All @@ -104,7 +104,7 @@ - (NSArray *)drop:(NSUInteger)n
return [a freeze];
}

- (NSArray *)dropWhile:(BOOL (^)(id obj))block
- (NSArray *)dropWhile:(MDElementFilter)block
{
NSMutableArray *a = [NSMutableArray arrayWithCapacity:[self count]/2];
return drop_foreach(self, a, block);
Expand All @@ -120,7 +120,7 @@ - (id)min:(NSComparator)block
return find_min(self, block);
}

- (MDPair *)partition:(BOOL (^)(id obj))block
- (MDPair *)partition:(MDElementFilter)block
{
NSMutableArray *trueVals = [NSMutableArray arrayWithCapacity:[self count]/2];
NSMutableArray *falseVals = [NSMutableArray arrayWithCapacity:[self count]/2];
Expand All @@ -136,7 +136,7 @@ - (NSArray *)take:(NSUInteger)n
return [a freeze];
}

- (NSArray *)takeWhile:(BOOL (^)(id obj))block
- (NSArray *)takeWhile:(MDElementFilter)block
{
NSMutableArray *a = [NSMutableArray arrayWithCapacity:[self count]];
return take_foreach(self, a, block);
Expand Down

0 comments on commit ddb29a8

Please sign in to comment.