8 be provided or otherwise made available to, or used by, any |
8 be provided or otherwise made available to, or used by, any |
9 other person. No title to or ownership of the software is |
9 other person. No title to or ownership of the software is |
10 hereby transferred. |
10 hereby transferred. |
11 " |
11 " |
12 |
12 |
|
13 "{ Package: 'stx:libbasic' }" |
|
14 |
13 Object subclass:#Magnitude |
15 Object subclass:#Magnitude |
14 instanceVariableNames:'' |
16 instanceVariableNames:'' |
15 classVariableNames:'' |
17 classVariableNames:'' |
16 poolDictionaries:'' |
18 poolDictionaries:'' |
17 category:'Magnitude-General' |
19 category:'Magnitude-General' |
125 2.0 min: 1.0 |
127 2.0 min: 1.0 |
126 2.0 min: 2 |
128 2.0 min: 2 |
127 " |
129 " |
128 ! ! |
130 ! ! |
129 |
131 |
|
132 !Magnitude methodsFor:'iteration'! |
|
133 |
|
134 to:stop by:incr do:aBlock |
|
135 "For each element of the interval from the receiver up to the argument stop, incrementing |
|
136 by step, evaluate aBlock passing the element as argument." |
|
137 |
|
138 |tmp| |
|
139 |
|
140 tmp := self. |
|
141 (incr > 0) ifTrue:[ |
|
142 [tmp <= stop] whileTrue:[ |
|
143 aBlock value:tmp. |
|
144 tmp := tmp+incr |
|
145 ] |
|
146 ] ifFalse:[ |
|
147 [tmp >= stop] whileTrue:[ |
|
148 aBlock value:tmp. |
|
149 tmp := tmp+incr |
|
150 ] |
|
151 ] |
|
152 ! |
|
153 |
|
154 to:stop by:incr doWithBreak:aBlock |
|
155 "For each element of the interval from the receiver up to the argument stop, incrementing |
|
156 by step, evaluate aBlock passing the element as argument. |
|
157 Pass a break argument, to allow for premature exit of the loop." |
|
158 |
|
159 |tmp break| |
|
160 |
|
161 break := [^ self]. |
|
162 tmp := self. |
|
163 (incr > 0) ifTrue:[ |
|
164 [tmp <= stop] whileTrue:[ |
|
165 aBlock value:tmp value:break. |
|
166 tmp := tmp+incr |
|
167 ] |
|
168 ] ifFalse:[ |
|
169 [tmp >= stop] whileTrue:[ |
|
170 aBlock value:tmp value:break. |
|
171 tmp := tmp+incr |
|
172 ] |
|
173 ] |
|
174 |
|
175 " |
|
176 1 to:100 by:5 doWithBreak:[:index :break | |
|
177 Transcript showCR:index printString. |
|
178 index > 50 ifTrue:[ |
|
179 break value |
|
180 ]. |
|
181 ] |
|
182 " |
|
183 ! |
|
184 |
|
185 to:stop by:incr doWithExit:aBlock |
|
186 "For each element of the interval from the receiver up to the argument stop, incrementing |
|
187 by step, evaluate aBlock passing the element as argument. |
|
188 Pass a break argument, to allow for premature exit of the loop." |
|
189 |
|
190 |tmp exit| |
|
191 |
|
192 exit := [:exitValue | ^ exitValue]. |
|
193 tmp := self. |
|
194 (incr > 0) ifTrue:[ |
|
195 [tmp <= stop] whileTrue:[ |
|
196 aBlock value:tmp value:exit. |
|
197 tmp := tmp+incr |
|
198 ] |
|
199 ] ifFalse:[ |
|
200 [tmp >= stop] whileTrue:[ |
|
201 aBlock value:tmp value:exit. |
|
202 tmp := tmp+incr |
|
203 ] |
|
204 ] |
|
205 |
|
206 " |
|
207 1 to:100 by:5 doWithExit:[:index :exit | |
|
208 Transcript showCR:index printString. |
|
209 index > 50 ifTrue:[ |
|
210 exit value:nil |
|
211 ]. |
|
212 ] |
|
213 " |
|
214 ! |
|
215 |
|
216 to:stop do:aBlock |
|
217 "For each element of the interval from the receiver up to the argument stop, |
|
218 evaluate aBlock, passing the number as argument." |
|
219 |
|
220 |tmp| |
|
221 |
|
222 tmp := self. |
|
223 [tmp <= stop] whileTrue:[ |
|
224 aBlock value:tmp. |
|
225 tmp := tmp+1 |
|
226 ] |
|
227 ! |
|
228 |
|
229 to:stop doWithBreak:aBlock |
|
230 "For each element of the interval from the receiver up to the argument stop, |
|
231 evaluate aBlock, passing the number as argument. |
|
232 Pass a break argument, to allow for premature exit of the loop." |
|
233 |
|
234 |tmp break| |
|
235 |
|
236 break := [^ self]. |
|
237 tmp := self. |
|
238 [tmp <= stop] whileTrue:[ |
|
239 aBlock value:tmp value:break. |
|
240 tmp := tmp+1 |
|
241 ] |
|
242 |
|
243 " |
|
244 1 to:10 doWithBreak:[:index :break | |
|
245 Transcript showCR:index printString. |
|
246 index > 5 ifTrue:[ |
|
247 break value |
|
248 ]. |
|
249 ] |
|
250 " |
|
251 ! |
|
252 |
|
253 to:stop doWithExit:aBlock |
|
254 "For each element of the interval from the receiver up to the argument stop, |
|
255 evaluate aBlock, passing the number as argument. |
|
256 An exitBlock is passed as second argument, which allows for the loop to be terminated early." |
|
257 |
|
258 |exit tmp| |
|
259 |
|
260 exit := [:exitValue | ^exitValue]. |
|
261 tmp := self. |
|
262 [tmp <= stop] whileTrue:[ |
|
263 aBlock value:tmp value:exit. |
|
264 tmp := tmp+1 |
|
265 ] |
|
266 |
|
267 " |
|
268 1 to:10 doWithExit:[:index :exit | |
|
269 index == 5 ifTrue:[ |
|
270 exit value:nil |
|
271 ]. |
|
272 Transcript showCR:index. |
|
273 ]. |
|
274 " |
|
275 ! ! |
|
276 |
130 !Magnitude methodsFor:'testing'! |
277 !Magnitude methodsFor:'testing'! |
131 |
278 |
132 between:min and:max |
279 between:min and:max |
133 "return whether the receiver is less than or equal to the argument max |
280 "return whether the receiver is less than or equal to the argument max |
134 and greater than or equal to the argument min." |
281 and greater than or equal to the argument min." |