Skip to content

Instantly share code, notes, and snippets.

@plantpurecode
Created October 11, 2012 09:07
Show Gist options
  • Save plantpurecode/3871152 to your computer and use it in GitHub Desktop.
Save plantpurecode/3871152 to your computer and use it in GitHub Desktop.
typedef void (^JRIterationBlock)(NSUInteger index);
static inline void invokeBlockIterativelyUsingRange(JRIterationBlock block, NSRange rng) {
NSParameterAssert(block);
for(NSUInteger i = rng.location; i < NSMaxRange(rng); ++i) {
block(i);
}
}
@implementation NSNumber (BlockAdditions)
- (void)times:(JRIterationBlock)block {
invokeBlockIterativelyUsingRange(block, (NSRange) {0, [self unsignedIntegerValue]});
}
- (void)upTo:(NSUInteger)upperBound do:(JRIterationBlock)block {
NSUInteger value = [self unsignedIntegerValue];
invokeBlockIterativelyUsingRange(block, (NSRange) {value, MAX(value, upperBound)});
}
@end
static inline void invokeBlockIterativelyUsingRange(void(^)(NSUInteger index)block, NSRange rng) {
NSParameterAssert(block);
for(NSUInteger i = rng.location; i < NSMaxRange(rng); ++i) {
block(i);
}
}
@implementation NSNumber (BlockAdditions)
- (void)times:(void(^)(NSUInteger index))block {
invokeBlockIterativelyUsingRange(block, (NSRange) {0, [self unsignedIntegerValue]});
}
- (void)upTo:(NSUInteger)upperBound do:(void(^)(NSUInteger index))block {
NSUInteger value = [self unsignedIntegerValue];
invokeBlockIterativelyUsingRange(block, (NSRange) {value, MAX(value, upperBound)});
}
@end
typedef void (^JRIterationBlock)(NSUInteger index);
static inline void invokeBlockIterativelyUsingRange(JRIterationBlock block, NSRange rng) {
NSParameterAssert(block);
for(NSUInteger i = rng.location; i < NSMaxRange(rng); ++i) {
block(i);
}
}
@implementation NSNumber (BlockAdditions)
- (void)times:(JRIterationBlock)block {
invokeBlockIterativelyUsingRange(block, (NSRange) {0, [self unsignedIntegerValue]});
}
- (void)upTo:(NSUInteger)upperBound do:(JRIterationBlock)block {
NSUInteger value = [self unsignedIntegerValue];
invokeBlockIterativelyUsingRange(block, (NSRange) {value, MAX(value, upperBound)});
}
@end
static inline void invokeBlockIterativelyUsingRange(void(^)(NSUInteger index)block, NSRange rng) {
NSParameterAssert(block);
for(NSUInteger i = rng.location; i < NSMaxRange(rng); ++i) {
block(i);
}
}
@implementation NSNumber (BlockAdditions)
- (void)times:(void(^)(NSUInteger index))block {
invokeBlockIterativelyUsingRange(block, (NSRange) {0, [self unsignedIntegerValue]});
}
- (void)upTo:(NSUInteger)upperBound do:(void(^)(NSUInteger index))block {
NSUInteger value = [self unsignedIntegerValue];
invokeBlockIterativelyUsingRange(block, (NSRange) {value, MAX(value, upperBound)});
}
@end
[@(5) times:^(NSUInteger index) {
NSLog(@"%u", index);
}];
[@(5) upTo:100 do:^(NSUInteger index) {
NSLog(@"%u", index);
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment