Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 85 lines (62 sloc) 1.689 kB
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
1 //
2 // NSSet+BlocksKit.m
3 // BlocksKit
4 //
5
6 #import "NSSet+BlocksKit.h"
7
32828a4 @zwaldowski None of you lot saw that.
authored
8 @implementation NSSet (BlocksKit)
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
9
90a62ca @zwaldowski A major misconception with the `typedef`-ing of a block. Now uses a …
authored
10 - (void)each:(BKSenderBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
11 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
12
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
13 [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
14 block(obj);
15 }];
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
16 }
17
ad5620e @zwaldowski Introduce the -apply: method for all collections. Fixes issue #14.
authored
18 - (void)apply:(BKSenderBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
19 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
20
ad5620e @zwaldowski Introduce the -apply: method for all collections. Fixes issue #14.
authored
21 [self enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, BOOL *stop) {
22 block(obj);
23 }];
24 }
25
90a62ca @zwaldowski A major misconception with the `typedef`-ing of a block. Now uses a …
authored
26 - (id)match:(BKValidationBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
27 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
28
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
29 return [[self objectsPassingTest:^BOOL(id obj, BOOL *stop) {
30 if (block(obj)) {
31 *stop = YES;
32 return YES;
33 }
34 return NO;
35 }] anyObject];
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
36 }
37
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
38 - (NSSet *)select:(BKValidationBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
39 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
40
903096d select and reject return empty array/dictionary/set instead of nil, to
Paul Bridger authored
41 return [self objectsPassingTest:^BOOL(id obj, BOOL *stop) {
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
42 return (block(obj));
43 }];
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
44 }
45
90a62ca @zwaldowski A major misconception with the `typedef`-ing of a block. Now uses a …
authored
46 - (NSSet *)reject:(BKValidationBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
47 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
48
903096d select and reject return empty array/dictionary/set instead of nil, to
Paul Bridger authored
49 return [self objectsPassingTest:^BOOL(id obj, BOOL *stop) {
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
50 return (!block(obj));
51 }];
38616d6 @zwaldowski Add a "reject" function (the opposite of select; no, really, it's jus…
authored
52 }
53
90a62ca @zwaldowski A major misconception with the `typedef`-ing of a block. Now uses a …
authored
54 - (NSSet *)map:(BKTransformBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
55 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
56
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
57 NSMutableSet *result = [[NSMutableSet alloc] initWithCapacity:self.count];
58
59 [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
60 id value = block(obj);
61 if (!value)
62 value = [NSNull null];
63
64 [result addObject:value];
65 }];
66636e4 @zwaldowski Migrate to building the Apple-provided blocks utilities for NSArray, …
authored
66
9845be3 @zwaldowski Go fully MRC (for safety reasons) in 1.0.
authored
67 return [result autorelease];
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
68 }
69
90a62ca @zwaldowski A major misconception with the `typedef`-ing of a block. Now uses a …
authored
70 - (id)reduce:(id)initial withBlock:(BKAccumulationBlock)block {
9a9f895 @a2 Replace `NSParameterAssert(block)` with `NSParameterAssert(block != n…
a2 authored
71 NSParameterAssert(block != nil);
eccc522 @zwaldowski Put NSParameterAssert all over the place to prevent crashes, exceptio…
authored
72
9845be3 @zwaldowski Go fully MRC (for safety reasons) in 1.0.
authored
73 __block id result = [initial retain];
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
74
75 [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
9845be3 @zwaldowski Go fully MRC (for safety reasons) in 1.0.
authored
76 id new = block(result, obj);
77 [result release];
78 result = [new retain];
8247525 @a2 Normalize line indentation to 1 tab
a2 authored
79 }];
80
9845be3 @zwaldowski Go fully MRC (for safety reasons) in 1.0.
authored
81 return [result autorelease];
78fae02 @zwaldowski Mutate NSArray extensions into NSSet ones.
authored
82 }
83
84 @end
Something went wrong with that request. Please try again.