Created
October 5, 2014 11:34
-
-
Save douglashill/b7e578b5955b5a927f03 to your computer and use it in GitHub Desktop.
Fold and reduce for NSArray. Mostly an academic exercise.
This file contains hidden or 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
| @import Foundation; | |
| @implementation NSArray (DHAccumulate) | |
| - (id)dh_objectByReducingWithOptions:(NSEnumerationOptions)options usingBlock:(id (^)(id object1, id object2))reductionBlock | |
| { | |
| if (options & NSEnumerationConcurrent) { | |
| [NSException raise:NSInvalidArgumentException format:@"Bad things will happen if you try to reduce an array concurrently."]; | |
| } | |
| __block id accumulatedValue; | |
| [self enumerateObjectsWithOptions:options usingBlock:^(id object, NSUInteger idx, BOOL *stop) { | |
| if (accumulatedValue == nil) { | |
| accumulatedValue = object; | |
| return; | |
| } | |
| accumulatedValue = reductionBlock(accumulatedValue, object); | |
| if (accumulatedValue == nil) { | |
| [NSException raise:NSInvalidArgumentException format:@"@s - reductionBlock must not return nil."]; | |
| } | |
| }]; | |
| if (accumulatedValue == nil) { | |
| [NSException raise:NSInvalidArgumentException format:@"%s must not be called on an empty array.", __PRETTY_FUNCTION__]; | |
| } | |
| return accumulatedValue; | |
| } | |
| - (id)dh_valueByFoldingWithInitialValue:(id)initialValue options:(NSEnumerationOptions)options usingBlock:(id (^)(id accumulatedValue, id object))foldBlock | |
| { | |
| if (options & NSEnumerationConcurrent) { | |
| [NSException raise:NSInvalidArgumentException format:@"Bad things will happen if you try to fold an array concurrently."]; | |
| } | |
| __block id accumulatedValue = initialValue; | |
| [self enumerateObjectsWithOptions:options usingBlock:^(id object, NSUInteger idx, BOOL *stop) { | |
| accumulatedValue = foldBlock(accumulatedValue, object); | |
| }]; | |
| return accumulatedValue; | |
| } | |
| - (NSInteger)dh_integerByFoldingWithInitialValue:(NSInteger)initialValue options:(NSEnumerationOptions)options usingBlock:(NSInteger (^)(NSInteger accumulatedValue, id object))foldBlock | |
| { | |
| if (options & NSEnumerationConcurrent) { | |
| [NSException raise:NSInvalidArgumentException format:@"Bad things will happen if you try to fold an array concurrently."]; | |
| } | |
| __block NSInteger accumulatedValue = initialValue; | |
| [self enumerateObjectsWithOptions:options usingBlock:^(id object, NSUInteger idx, BOOL *stop) { | |
| accumulatedValue = foldBlock(accumulatedValue, object); | |
| }]; | |
| return accumulatedValue; | |
| } | |
| - (BOOL)dh_booleanByFoldingWithInitialValue:(BOOL)initialValue options:(NSEnumerationOptions)options usingBlock:(BOOL (^)(BOOL accumulatedValue, id object))foldBlock | |
| { | |
| if (options & NSEnumerationConcurrent) { | |
| [NSException raise:NSInvalidArgumentException format:@"Bad things will happen if you try to fold an array concurrently."]; | |
| } | |
| __block BOOL accumulatedValue = initialValue; | |
| [self enumerateObjectsWithOptions:options usingBlock:^(id object, NSUInteger idx, BOOL *stop) { | |
| accumulatedValue = foldBlock(accumulatedValue, object); | |
| }]; | |
| return accumulatedValue; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment