1 |
/** |
2 |
NSNumber-additions.h |
3 |
Various methods for NSNumber |
4 |
|
5 |
Copyright (c) 2002 Free Software Foundation |
6 |
|
7 |
This file is part of the StepTalk project. |
8 |
|
9 |
*/ |
10 |
|
11 |
#import "NSNumber+additions.h" |
12 |
#import "STBlock.h" |
13 |
#include <math.h> |
14 |
#import <Foundation/NSAutoreleasePool.h> |
15 |
|
16 |
@implementation NSNumber (STSmalltalkAdditions) |
17 |
- ifTrue:(STBlock *)block |
18 |
{ |
19 |
if([self isTrue]) |
20 |
{ |
21 |
return [block value]; |
22 |
} |
23 |
return self; |
24 |
} |
25 |
- ifFalse:(STBlock *)block |
26 |
{ |
27 |
if([self isFalse]) |
28 |
{ |
29 |
return [block value]; |
30 |
} |
31 |
return self; |
32 |
} |
33 |
- ifTrue:(STBlock *)trueBlock ifFalse:(STBlock *)falseBlock |
34 |
{ |
35 |
if([self isTrue]) |
36 |
{ |
37 |
return [trueBlock value]; |
38 |
} |
39 |
return [falseBlock value]; |
40 |
} |
41 |
- ifFalse:(STBlock *)falseBlock ifTrue:(STBlock *)trueBlock |
42 |
{ |
43 |
if([self isFalse]) |
44 |
{ |
45 |
return [falseBlock value]; |
46 |
} |
47 |
return [trueBlock value]; |
48 |
} |
49 |
|
50 |
- (BOOL)isTrue |
51 |
{ |
52 |
return ([self intValue] != 0); |
53 |
} |
54 |
- (BOOL)isFalse |
55 |
{ |
56 |
return ([self intValue] == 0); |
57 |
} |
58 |
|
59 |
/* FIXME: make it work with floats */ |
60 |
|
61 |
- to:(int)number do:(STBlock *)block |
62 |
{ |
63 |
id retval = nil; |
64 |
int i; |
65 |
|
66 |
for(i=[self intValue];i<=number;i++) |
67 |
{ |
68 |
retval = [block valueWith:[NSNumber numberWithInt:i]]; |
69 |
} |
70 |
return retval; |
71 |
} |
72 |
|
73 |
- to:(int)number step:(int)step do:(STBlock *)block |
74 |
{ |
75 |
id retval = nil; |
76 |
int i; |
77 |
|
78 |
if (step > 0) |
79 |
{ |
80 |
for(i=[self intValue];i<=number;i+=step) |
81 |
{ |
82 |
retval = [block valueWith:[NSNumber numberWithInt:i]]; |
83 |
} |
84 |
} |
85 |
else |
86 |
{ |
87 |
// step =< 0 |
88 |
for(i=[self intValue];i>=number;i+=step) |
89 |
{ |
90 |
retval = [block valueWith:[NSNumber numberWithInt:i]]; |
91 |
} |
92 |
} |
93 |
|
94 |
return retval; |
95 |
} |
96 |
@end |