Last active
September 23, 2015 07:36
-
-
Save jakubpetrik/78fb361dbd815ccae3ca to your computer and use it in GitHub Desktop.
Reducing BOOLs.
This file contains 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
@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