Skip to content

Instantly share code, notes, and snippets.

@jakubpetrik
Last active September 23, 2015 07:36
Show Gist options
  • Save jakubpetrik/78fb361dbd815ccae3ca to your computer and use it in GitHub Desktop.
Save jakubpetrik/78fb361dbd815ccae3ca to your computer and use it in GitHub Desktop.
Reducing BOOLs.
@interface Reducer : NSObject
+ (id(^)())reduceToBoolMultiple:(NSNumber *)boolArgs, ... NS_REQUIRES_NIL_TERMINATION;
+ (id(^)())reduceToBoolArray:(NSArray *)array;
@end
@implementation Reducer
+ (id (^)())reduceToBoolMultiple:(NSNumber *)firstArg, ... NS_REQUIRES_NIL_TERMINATION {
BOOL retVal = firstArg.boolValue;
va_list args;
va_start(args, firstArg);
for (NSNumber *arg = firstArg; arg; arg = va_arg(args, NSNumber *)) {
retVal = arg.boolValue && retVal;
}
va_end(args);
return ^id {
return @(retVal);
};
}
+ (id (^)())reduceToBoolArray:(NSArray *)array {
BOOL retVal = [[array firstObject] boolValue];
for (int i = 1; i < array.count; i++) {
retVal = [array[i] boolValue] && retVal;
}
return ^id {
return @(retVal);
};
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment