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