March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -15,19 +15,18 @@
if (i < 2)
result = 1;
else
result = [[self performSelector:_cmd withObject:[NSNumber numberWithInt:i-1]] intValue]
+ [[self performSelector:_cmd withObject:[NSNumber numberWithInt:i-2]] intValue];
return [NSNumber numberWithInt:result];
result = [[self performSelector:_cmd withObject:@(i-1)] intValue]
+ [[self performSelector:_cmd withObject:@(i-2)] intValue];
return @(result);
}
@end
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
AnonymousRecursion *dummy = [[AnonymousRecursion alloc] init];
NSLog(@"%@", [dummy fibonacci:[NSNumber numberWithInt:8]]);
[dummy release];
AnonymousRecursion *dummy = [[AnonymousRecursion alloc] init];
NSLog(@"%@", [dummy fibonacci:@8]);
[pool release];
}
return 0;
}

View file

@ -5,21 +5,22 @@ int fib(int n) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"fib: no negative numbers"
userInfo:nil];
__block int (^f)(int);
f = ^(int n) {
int (^f)(int);
__block __weak int (^weak_f)(int); // block cannot capture strong reference to itself
weak_f = f = ^(int n) {
if (n < 2)
return 1;
else
return f(n-1) + f(n-2);
return weak_f(n-1) + weak_f(n-2);
};
return f(n);
}
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSLog(@"%d", fib(8));
NSLog(@"%d", fib(8));
[pool release];
}
return 0;
}

View file

@ -0,0 +1,25 @@
#import <Foundation/Foundation.h>
int fib(int n) {
if (n < 0)
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"fib: no negative numbers"
userInfo:nil];
__block int (^f)(int);
f = ^(int n) {
if (n < 2)
return 1;
else
return f(n-1) + f(n-2);
};
return f(n);
}
int main (int argc, const char *argv[]) {
@autoreleasepool {
NSLog(@"%d", fib(8));
}
return 0;
}