author | Claus Gittinger <cg@exept.de> |
Sun, 05 Jan 2020 17:27:00 +0100 | |
changeset 25177 | 1c618f3d51f5 |
parent 24468 | 3bf34e79ef6e |
permissions | -rw-r--r-- |
24396 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1988 by Claus Gittinger |
213 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
15 |
|
18396 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
Object subclass:#Magnitude |
1226 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Magnitude-General' |
|
1 | 23 |
! |
24 |
||
88 | 25 |
!Magnitude class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
213 | 30 |
All Rights Reserved |
1 | 31 |
|
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
21838 | 43 |
This is an abstract class defining common methods for |
21154 | 44 |
Objects which can be compared by a kind of less-than relation. |
1295 | 45 |
|
46 |
[author:] |
|
47 |
Claus Gittinger |
|
88 | 48 |
" |
49 |
! ! |
|
1 | 50 |
|
8892 | 51 |
!Magnitude class methodsFor:'queries'! |
52 |
||
53 |
isAbstract |
|
11226 | 54 |
"Return if this class is an abstract class. |
55 |
True is returned for Magnitude here; false for subclasses. |
|
19456 | 56 |
Abstract subclasses must redefine this again." |
11226 | 57 |
|
8892 | 58 |
^ self == Magnitude |
59 |
! ! |
|
60 |
||
10600 | 61 |
!Magnitude methodsFor:'Compatibility-Squeak'! |
62 |
||
63 |
min:aMin max:aMax |
|
64 |
"similar to clampBetween:and:" |
|
65 |
||
66 |
^ (self min: aMin) max: aMax |
|
67 |
||
68 |
"Created: / 06-06-2007 / 11:00:22 / cg" |
|
24468 | 69 |
! |
70 |
||
71 |
threeWayCompareWith:arg |
|
72 |
"alias for compareWith:" |
|
73 |
||
74 |
^ self compareWith:arg |
|
75 |
||
76 |
"Created: / 24-07-2019 / 10:10:08 / Claus Gittinger" |
|
10600 | 77 |
! ! |
78 |
||
1 | 79 |
!Magnitude methodsFor:'comparing'! |
80 |
||
699 | 81 |
< aMagnitude |
1 | 82 |
"Compare the receiver with the argument and return true if the |
699 | 83 |
receiver is less than the argument. Otherwise return false." |
84 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
85 |
^ self subclassResponsibility |
699 | 86 |
! |
1 | 87 |
|
699 | 88 |
<= aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
89 |
"return true, if the argument is greater or equal than the receiver" |
699 | 90 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
91 |
^ (aMagnitude < self) not |
1 | 92 |
! |
93 |
||
94 |
= aMagnitude |
|
95 |
"Compare the receiver with the argument and return true if the |
|
96 |
receiver is equal to the argument. Otherwise return false." |
|
97 |
||
98 |
^ self subclassResponsibility |
|
99 |
! |
|
100 |
||
699 | 101 |
> aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
102 |
"return true, if the argument is less than the receiver" |
1 | 103 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
104 |
^ aMagnitude < self |
1 | 105 |
! |
106 |
||
107 |
>= aMagnitude |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
108 |
"return true, if the argument is less or equal than the receiver" |
1 | 109 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
110 |
^ (self < aMagnitude) not |
213 | 111 |
! |
112 |
||
1226 | 113 |
clampBetween:min and:max |
21838 | 114 |
"return the receiver if it is between min .. max, |
115 |
or min if it is less than min, or max if it is greater than max. |
|
1226 | 116 |
This is only a lazy-typers helper for: ((something min:max) max:min)" |
117 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
118 |
(max < self) ifTrue:[^ max]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
119 |
(self < min) ifTrue:[^ min]. |
1226 | 120 |
^ self |
121 |
||
122 |
" |
|
123 |
1 clampBetween:2 and:5 |
|
124 |
3 clampBetween:2 and:5 |
|
125 |
6 clampBetween:2 and:5 |
|
126 |
" |
|
127 |
||
21445 | 128 |
"Modified: / 19-04-1996 / 14:58:12 / cg" |
129 |
"Modified (comment): / 13-02-2017 / 20:26:41 / cg" |
|
21838 | 130 |
"Modified (comment): / 19-06-2017 / 14:59:47 / mawalch" |
1226 | 131 |
! |
132 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
133 |
compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
134 |
"three-way compare - thanks to Self for this idea. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
135 |
Can be redefined in subclasses to do it with a single comparison if |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
136 |
comparison is expensive." |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
137 |
|
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
138 |
self < arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
139 |
^ lessBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
140 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
141 |
self = arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
142 |
^ equalBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
143 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
144 |
^ greaterBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
145 |
! |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
146 |
|
19006 | 147 |
compareWith:arg |
148 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
149 |
greater, 0 if equal and -1 if less than the argument." |
|
150 |
||
151 |
self < arg ifTrue:[ |
|
152 |
^ -1 |
|
153 |
]. |
|
154 |
self = arg ifTrue:[ |
|
155 |
^ 0 |
|
156 |
]. |
|
157 |
^ 1 |
|
158 |
||
159 |
" |
|
160 |
100 compareWith: 101 |
|
161 |
" |
|
162 |
! |
|
163 |
||
16275 | 164 |
hash |
165 |
"instances, for which #= answers true must answer the same hash" |
|
166 |
||
167 |
^ self subclassResponsibility |
|
168 |
! |
|
169 |
||
699 | 170 |
max:aMagnitude |
171 |
"return the receiver or the argument, whichever has greater magnitude" |
|
172 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
173 |
(aMagnitude < self) ifTrue:[^ self]. |
699 | 174 |
^ aMagnitude |
175 |
||
176 |
" |
|
177 |
1 max: 2 |
|
178 |
1 max: 2.0 |
|
179 |
2.0 max: 1.0 |
|
180 |
2.0 max: 2 |
|
181 |
" |
|
182 |
! |
|
183 |
||
213 | 184 |
min:aMagnitude |
185 |
"return the receiver or the argument, whichever has lesser magnitude" |
|
186 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
187 |
(self < aMagnitude) ifTrue:[^ self]. |
213 | 188 |
^ aMagnitude |
189 |
||
190 |
" |
|
191 |
1 min: 2 |
|
192 |
1 min: 2.0 |
|
193 |
2.0 min: 1.0 |
|
194 |
2.0 min: 2 |
|
195 |
" |
|
1 | 196 |
! ! |
197 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
198 |
!Magnitude methodsFor:'iteration'! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
199 |
|
11602 | 200 |
downTo:stop by:step do:aBlock |
201 |
"For each element of the interval from the receiver down to the argument stop, |
|
202 |
decrementing by step, evaluate aBlock, passing the number as argument." |
|
203 |
||
204 |
^ self to:stop by:step negated do:aBlock |
|
205 |
||
206 |
" |
|
207 |
10 downTo:1 by:2 do:[:i | Transcript showCR:i]. |
|
208 |
" |
|
209 |
! |
|
210 |
||
6073 | 211 |
downTo:stop do:aBlock |
212 |
"For each element of the interval from the receiver down to the argument stop, |
|
213 |
evaluate aBlock, passing the number as argument." |
|
214 |
||
215 |
^ self to:stop by:-1 do:aBlock |
|
216 |
||
217 |
" |
|
218 |
10 downTo:1 do:[:i | Transcript showCR:i]. |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
219 |
$d downTo:$a do:[:i | Transcript showCR:i]. |
6073 | 220 |
" |
221 |
! |
|
222 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
223 |
to:stop by:incr do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
224 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
225 |
by step, evaluate aBlock passing the element as argument. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
226 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
227 |
Not all Magnitudes do implement #negative and #+ however, |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
228 |
so should this method go to ArithmethicValue? |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
229 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
230 |
Only use #<, to speed up things (for subclasses only defining #<)" |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
231 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
232 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
233 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
234 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
235 |
"using #negative would be more portable, but stc does only inline #< but not #negative (yet)" |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
236 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
237 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
238 |
[tmp < stop] whileFalse:[ |
6073 | 239 |
aBlock value:tmp. |
240 |
tmp := tmp+incr |
|
241 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
242 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
243 |
[stop < tmp] whileFalse:[ |
6073 | 244 |
aBlock value:tmp. |
245 |
tmp := tmp+incr |
|
246 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
247 |
] |
6073 | 248 |
|
249 |
" |
|
250 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
251 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
252 |
$a to:$z by:2 do:[:i | Transcript showCR:i]. |
6073 | 253 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
254 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
255 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
256 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
257 |
to:stop by:incr doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
258 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
259 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
260 |
Pass a break argument, to allow for premature exit of the loop." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
261 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
262 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
263 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
264 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
265 |
tmp := self. |
24396 | 266 |
"using #negative would be more portable, but stc does only inline #< but not #negative (yet)" |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
267 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
268 |
[tmp < stop] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
269 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
270 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
271 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
272 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
273 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
274 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
275 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
276 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
277 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
278 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
279 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
280 |
1 to:100 by:5 doWithBreak:[:index :break | |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
281 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
282 |
index > 50 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
283 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
284 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
285 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
286 |
" |
24396 | 287 |
|
288 |
"Modified (comment): / 28-06-2019 / 12:44:35 / Claus Gittinger" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
289 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
290 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
291 |
to:stop by:incr doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
292 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
293 |
by step, evaluate aBlock passing the element as argument. |
24396 | 294 |
Pass a break argument, to allow for premature exit of the loop. |
295 |
Returns nil or the value passed to the exit>>value: message. |
|
296 |
||
297 |
Notice, that this is different to a return statement in the block, |
|
298 |
which returns from the enclosed method, NOT only from the block." |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
299 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
300 |
|tmp exit| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
301 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
302 |
exit := [:exitValue | ^ exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
303 |
tmp := self. |
24396 | 304 |
"/ incr negative ifTrue:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
305 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
306 |
[tmp < stop] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
307 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
308 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
309 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
310 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
311 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
312 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
313 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
314 |
] |
24396 | 315 |
]. |
316 |
^ nil |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
317 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
318 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
319 |
1 to:100 by:5 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
320 |
Transcript showCR:index printString. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
321 |
index > 50 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
322 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
323 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
324 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
325 |
" |
24396 | 326 |
|
327 |
"Modified: / 28-06-2019 / 12:46:35 / Claus Gittinger" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
328 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
329 |
|
11837 | 330 |
to:stop count:aBlock |
331 |
"same as (self to:stop) count:aBlock" |
|
332 |
||
333 |
|cnt| |
|
334 |
||
335 |
cnt := 0. |
|
336 |
self to:stop do:[:i | |
|
337 |
(aBlock value:i) ifTrue:[ cnt := cnt + 1 ]. |
|
338 |
]. |
|
339 |
^ cnt |
|
340 |
||
341 |
" |
|
342 |
1 to:10 count:[:n | n even] |
|
343 |
" |
|
344 |
! |
|
345 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
346 |
to:stop do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
347 |
"For each element of the interval from the receiver up to the argument stop, |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
348 |
evaluate aBlock, passing the number as argument." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
349 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
350 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
351 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
352 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
353 |
[stop < tmp] whileFalse:[ |
6073 | 354 |
aBlock value:tmp. |
355 |
tmp := tmp+1 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
356 |
] |
6073 | 357 |
|
358 |
" |
|
359 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
360 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
361 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
|
362 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
363 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
364 |
|
21806 | 365 |
to:stop do:aBlock separatedBy:actionBetween |
366 |
"For each element of the interval from the receiver up to the argument stop, |
|
367 |
evaluate aBlock, passing the number as argument. |
|
368 |
Between each, evaluate actionBetween (but not before the first and not after the last)" |
|
369 |
||
370 |
|tmp first| |
|
371 |
||
372 |
tmp := self. |
|
373 |
first := true. |
|
374 |
[stop < tmp] whileFalse:[ |
|
375 |
first ifFalse:[ |
|
376 |
actionBetween value. |
|
377 |
]. |
|
378 |
first := false. |
|
379 |
aBlock value:tmp. |
|
380 |
tmp := tmp+1. |
|
381 |
] |
|
382 |
||
383 |
" |
|
384 |
1 to:10 |
|
385 |
do:[:i | Transcript show:i] |
|
386 |
separatedBy:[ Transcript show:', ']. |
|
387 |
" |
|
388 |
||
389 |
"Created: / 14-06-2017 / 15:23:21 / cg" |
|
390 |
! |
|
391 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
392 |
to:stop doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
393 |
"For each element of the interval from the receiver up to the argument stop, |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
394 |
evaluate aBlock, passing the number as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
395 |
Pass a break argument, to allow for premature exit of the loop." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
396 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
397 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
398 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
399 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
400 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
401 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
402 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
403 |
tmp := tmp+1 |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
404 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
405 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
406 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
407 |
1 to:10 doWithBreak:[:index :break | |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
408 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
409 |
index > 5 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
410 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
411 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
412 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
413 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
414 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
415 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
416 |
to:stop doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
417 |
"For each element of the interval from the receiver up to the argument stop, |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
418 |
evaluate aBlock, passing the number as argument. |
15374 | 419 |
Passes an additional exit object, which can be used to leave |
420 |
the loop early, by sending it a #value: message. |
|
421 |
Returns nil or the value passed to the exit>>value: message. |
|
422 |
||
423 |
Notice, that this is different to a return statement in the block, |
|
424 |
which returns from the enclosed method, NOT only from the block." |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
425 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
426 |
|exit tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
427 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
428 |
exit := [:exitValue | ^exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
429 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
430 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
431 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
432 |
tmp := tmp+1 |
15374 | 433 |
]. |
434 |
^ nil |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
435 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
436 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
437 |
1 to:10 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
438 |
index == 5 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
439 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
440 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
441 |
Transcript showCR:index. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
442 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
443 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
444 |
! ! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
445 |
|
1 | 446 |
!Magnitude methodsFor:'testing'! |
447 |
||
448 |
between:min and:max |
|
21838 | 449 |
"return true if the receiver is greater than or equal to the argument min |
18396 | 450 |
and less than or equal to the argument max" |
1 | 451 |
|
452 |
(self < min) ifTrue:[^ false]. |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
453 |
(max < self) ifTrue:[^ false]. |
1 | 454 |
^ true |
56 | 455 |
|
213 | 456 |
" |
457 |
1.2 between:1 and:2 |
|
458 |
(3/2) between:1 and:2 |
|
459 |
(3/2) between:0 and:1 |
|
460 |
" |
|
21838 | 461 |
|
462 |
"Modified (comment): / 19-06-2017 / 14:59:05 / mawalch" |
|
1 | 463 |
! |
464 |
||
465 |
in:anInterval |
|
466 |
"return whether the receiver is within the interval bounds" |
|
467 |
||
468 |
(self < anInterval start) ifTrue:[^ false]. |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
469 |
(anInterval stop < self) ifTrue:[^ false]. |
1 | 470 |
^ true |
56 | 471 |
|
213 | 472 |
" |
473 |
1.2 in:(1 to: 2) |
|
474 |
(3/2) in:(1 to: 2) |
|
475 |
(3/2) in:(0 to: 1) |
|
476 |
" |
|
1 | 477 |
! ! |
699 | 478 |
|
479 |
!Magnitude class methodsFor:'documentation'! |
|
480 |
||
481 |
version |
|
19005 | 482 |
^ '$Header$' |
19006 | 483 |
! |
484 |
||
485 |
version_CVS |
|
486 |
^ '$Header$' |
|
699 | 487 |
! ! |
15374 | 488 |