diff -r 0f513f04c50f -r a815d6b86ace Magnitude.st --- a/Magnitude.st Sun Aug 27 16:48:50 2000 +0200 +++ b/Magnitude.st Tue Aug 29 17:12:09 2000 +0200 @@ -10,6 +10,8 @@ hereby transferred. " +"{ Package: 'stx:libbasic' }" + Object subclass:#Magnitude instanceVariableNames:'' classVariableNames:'' @@ -127,6 +129,151 @@ " ! ! +!Magnitude methodsFor:'iteration'! + +to:stop by:incr do:aBlock + "For each element of the interval from the receiver up to the argument stop, incrementing + by step, evaluate aBlock passing the element as argument." + + |tmp| + + tmp := self. + (incr > 0) ifTrue:[ + [tmp <= stop] whileTrue:[ + aBlock value:tmp. + tmp := tmp+incr + ] + ] ifFalse:[ + [tmp >= stop] whileTrue:[ + aBlock value:tmp. + tmp := tmp+incr + ] + ] +! + +to:stop by:incr doWithBreak:aBlock + "For each element of the interval from the receiver up to the argument stop, incrementing + by step, evaluate aBlock passing the element as argument. + Pass a break argument, to allow for premature exit of the loop." + + |tmp break| + + break := [^ self]. + tmp := self. + (incr > 0) ifTrue:[ + [tmp <= stop] whileTrue:[ + aBlock value:tmp value:break. + tmp := tmp+incr + ] + ] ifFalse:[ + [tmp >= stop] whileTrue:[ + aBlock value:tmp value:break. + tmp := tmp+incr + ] + ] + + " + 1 to:100 by:5 doWithBreak:[:index :break | + Transcript showCR:index printString. + index > 50 ifTrue:[ + break value + ]. + ] + " +! + +to:stop by:incr doWithExit:aBlock + "For each element of the interval from the receiver up to the argument stop, incrementing + by step, evaluate aBlock passing the element as argument. + Pass a break argument, to allow for premature exit of the loop." + + |tmp exit| + + exit := [:exitValue | ^ exitValue]. + tmp := self. + (incr > 0) ifTrue:[ + [tmp <= stop] whileTrue:[ + aBlock value:tmp value:exit. + tmp := tmp+incr + ] + ] ifFalse:[ + [tmp >= stop] whileTrue:[ + aBlock value:tmp value:exit. + tmp := tmp+incr + ] + ] + + " + 1 to:100 by:5 doWithExit:[:index :exit | + Transcript showCR:index printString. + index > 50 ifTrue:[ + exit value:nil + ]. + ] + " +! + +to:stop do:aBlock + "For each element of the interval from the receiver up to the argument stop, + evaluate aBlock, passing the number as argument." + + |tmp| + + tmp := self. + [tmp <= stop] whileTrue:[ + aBlock value:tmp. + tmp := tmp+1 + ] +! + +to:stop doWithBreak:aBlock + "For each element of the interval from the receiver up to the argument stop, + evaluate aBlock, passing the number as argument. + Pass a break argument, to allow for premature exit of the loop." + + |tmp break| + + break := [^ self]. + tmp := self. + [tmp <= stop] whileTrue:[ + aBlock value:tmp value:break. + tmp := tmp+1 + ] + + " + 1 to:10 doWithBreak:[:index :break | + Transcript showCR:index printString. + index > 5 ifTrue:[ + break value + ]. + ] + " +! + +to:stop doWithExit:aBlock + "For each element of the interval from the receiver up to the argument stop, + evaluate aBlock, passing the number as argument. + An exitBlock is passed as second argument, which allows for the loop to be terminated early." + + |exit tmp| + + exit := [:exitValue | ^exitValue]. + tmp := self. + [tmp <= stop] whileTrue:[ + aBlock value:tmp value:exit. + tmp := tmp+1 + ] + + " + 1 to:10 doWithExit:[:index :exit | + index == 5 ifTrue:[ + exit value:nil + ]. + Transcript showCR:index. + ]. + " +! ! + !Magnitude methodsFor:'testing'! between:min and:max @@ -161,5 +308,5 @@ !Magnitude class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/Magnitude.st,v 1.14 1996-04-25 16:57:17 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/Magnitude.st,v 1.15 2000-08-29 15:12:09 cg Exp $' ! !