2002-05-13 22:13:06 +00:00
|
|
|
/**
|
|
|
|
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>
|
2003-09-24 16:52:29 +00:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2002-05-13 22:13:06 +00:00
|
|
|
|
|
|
|
@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;
|
|
|
|
|
2003-09-28 20:57:06 +00:00
|
|
|
if (step > 0)
|
2002-05-13 22:13:06 +00:00
|
|
|
{
|
2003-09-28 20:57:06 +00:00
|
|
|
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]];
|
|
|
|
}
|
2002-05-13 22:13:06 +00:00
|
|
|
}
|
2003-09-28 20:57:06 +00:00
|
|
|
|
2002-05-13 22:13:06 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
@end
|