author | Claus Gittinger <cg@exept.de> |
Wed, 10 Jul 2019 18:39:27 +0200 | |
changeset 24418 | 24eb2af1ca29 |
parent 24396 | 3dfb65acb1f0 |
child 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" |
|
69 |
! ! |
|
70 |
||
1 | 71 |
!Magnitude methodsFor:'comparing'! |
72 |
||
699 | 73 |
< aMagnitude |
1 | 74 |
"Compare the receiver with the argument and return true if the |
699 | 75 |
receiver is less than the argument. Otherwise return false." |
76 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
77 |
^ self subclassResponsibility |
699 | 78 |
! |
1 | 79 |
|
699 | 80 |
<= aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
81 |
"return true, if the argument is greater or equal than the receiver" |
699 | 82 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
83 |
^ (aMagnitude < self) not |
1 | 84 |
! |
85 |
||
86 |
= aMagnitude |
|
87 |
"Compare the receiver with the argument and return true if the |
|
88 |
receiver is equal to the argument. Otherwise return false." |
|
89 |
||
90 |
^ self subclassResponsibility |
|
91 |
! |
|
92 |
||
699 | 93 |
> aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
94 |
"return true, if the argument is less than the receiver" |
1 | 95 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
96 |
^ aMagnitude < self |
1 | 97 |
! |
98 |
||
99 |
>= aMagnitude |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
100 |
"return true, if the argument is less or equal than the receiver" |
1 | 101 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
102 |
^ (self < aMagnitude) not |
213 | 103 |
! |
104 |
||
1226 | 105 |
clampBetween:min and:max |
21838 | 106 |
"return the receiver if it is between min .. max, |
107 |
or min if it is less than min, or max if it is greater than max. |
|
1226 | 108 |
This is only a lazy-typers helper for: ((something min:max) max:min)" |
109 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
110 |
(max < self) ifTrue:[^ max]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
111 |
(self < min) ifTrue:[^ min]. |
1226 | 112 |
^ self |
113 |
||
114 |
" |
|
115 |
1 clampBetween:2 and:5 |
|
116 |
3 clampBetween:2 and:5 |
|
117 |
6 clampBetween:2 and:5 |
|
118 |
" |
|
119 |
||
21445 | 120 |
"Modified: / 19-04-1996 / 14:58:12 / cg" |
121 |
"Modified (comment): / 13-02-2017 / 20:26:41 / cg" |
|
21838 | 122 |
"Modified (comment): / 19-06-2017 / 14:59:47 / mawalch" |
1226 | 123 |
! |
124 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
125 |
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
|
126 |
"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
|
127 |
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
|
128 |
comparison is expensive." |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
129 |
|
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
130 |
self < arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
131 |
^ lessBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
132 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
133 |
self = arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
134 |
^ equalBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
135 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
136 |
^ greaterBlock value |
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 |
|
19006 | 139 |
compareWith:arg |
140 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
141 |
greater, 0 if equal and -1 if less than the argument." |
|
142 |
||
143 |
self < arg ifTrue:[ |
|
144 |
^ -1 |
|
145 |
]. |
|
146 |
self = arg ifTrue:[ |
|
147 |
^ 0 |
|
148 |
]. |
|
149 |
^ 1 |
|
150 |
||
151 |
" |
|
152 |
100 compareWith: 101 |
|
153 |
" |
|
154 |
! |
|
155 |
||
16275 | 156 |
hash |
157 |
"instances, for which #= answers true must answer the same hash" |
|
158 |
||
159 |
^ self subclassResponsibility |
|
160 |
! |
|
161 |
||
699 | 162 |
max:aMagnitude |
163 |
"return the receiver or the argument, whichever has greater magnitude" |
|
164 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
165 |
(aMagnitude < self) ifTrue:[^ self]. |
699 | 166 |
^ aMagnitude |
167 |
||
168 |
" |
|
169 |
1 max: 2 |
|
170 |
1 max: 2.0 |
|
171 |
2.0 max: 1.0 |
|
172 |
2.0 max: 2 |
|
173 |
" |
|
174 |
! |
|
175 |
||
213 | 176 |
min:aMagnitude |
177 |
"return the receiver or the argument, whichever has lesser magnitude" |
|
178 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
179 |
(self < aMagnitude) ifTrue:[^ self]. |
213 | 180 |
^ aMagnitude |
181 |
||
182 |
" |
|
183 |
1 min: 2 |
|
184 |
1 min: 2.0 |
|
185 |
2.0 min: 1.0 |
|
186 |
2.0 min: 2 |
|
187 |
" |
|
1 | 188 |
! ! |
189 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
190 |
!Magnitude methodsFor:'iteration'! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
191 |
|
11602 | 192 |
downTo:stop by:step do:aBlock |
193 |
"For each element of the interval from the receiver down to the argument stop, |
|
194 |
decrementing by step, evaluate aBlock, passing the number as argument." |
|
195 |
||
196 |
^ self to:stop by:step negated do:aBlock |
|
197 |
||
198 |
" |
|
199 |
10 downTo:1 by:2 do:[:i | Transcript showCR:i]. |
|
200 |
" |
|
201 |
! |
|
202 |
||
6073 | 203 |
downTo:stop do:aBlock |
204 |
"For each element of the interval from the receiver down to the argument stop, |
|
205 |
evaluate aBlock, passing the number as argument." |
|
206 |
||
207 |
^ self to:stop by:-1 do:aBlock |
|
208 |
||
209 |
" |
|
210 |
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
|
211 |
$d downTo:$a do:[:i | Transcript showCR:i]. |
6073 | 212 |
" |
213 |
! |
|
214 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
215 |
to:stop by:incr do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
216 |
"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
|
217 |
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
|
218 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
219 |
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
|
220 |
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
|
221 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
222 |
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
|
223 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
224 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
225 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
226 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
227 |
"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
|
228 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
229 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
230 |
[tmp < stop] whileFalse:[ |
6073 | 231 |
aBlock value:tmp. |
232 |
tmp := tmp+incr |
|
233 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
234 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
235 |
[stop < tmp] whileFalse:[ |
6073 | 236 |
aBlock value:tmp. |
237 |
tmp := tmp+incr |
|
238 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
239 |
] |
6073 | 240 |
|
241 |
" |
|
242 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
243 |
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
|
244 |
$a to:$z by:2 do:[:i | Transcript showCR:i]. |
6073 | 245 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
246 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
247 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
248 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
249 |
to:stop by:incr doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
250 |
"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
|
251 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
252 |
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
|
253 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
254 |
|tmp break| |
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 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
257 |
tmp := self. |
24396 | 258 |
"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
|
259 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
260 |
[tmp < stop] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
261 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
262 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
263 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
264 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
265 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
266 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
267 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
268 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
269 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
270 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
271 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
272 |
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
|
273 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
274 |
index > 50 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
275 |
break value |
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 |
" |
24396 | 279 |
|
280 |
"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
|
281 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
282 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
283 |
to:stop by:incr doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
284 |
"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
|
285 |
by step, evaluate aBlock passing the element as argument. |
24396 | 286 |
Pass a break argument, to allow for premature exit of the loop. |
287 |
Returns nil or the value passed to the exit>>value: message. |
|
288 |
||
289 |
Notice, that this is different to a return statement in the block, |
|
290 |
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
|
291 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
292 |
|tmp exit| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
293 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
294 |
exit := [:exitValue | ^ exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
295 |
tmp := self. |
24396 | 296 |
"/ incr negative ifTrue:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
297 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
298 |
[tmp < stop] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
299 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
300 |
tmp := tmp+incr |
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 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
303 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
304 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
305 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
306 |
] |
24396 | 307 |
]. |
308 |
^ nil |
|
5565
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 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
311 |
1 to:100 by:5 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
312 |
Transcript showCR:index printString. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
313 |
index > 50 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
314 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
315 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
316 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
317 |
" |
24396 | 318 |
|
319 |
"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
|
320 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
321 |
|
11837 | 322 |
to:stop count:aBlock |
323 |
"same as (self to:stop) count:aBlock" |
|
324 |
||
325 |
|cnt| |
|
326 |
||
327 |
cnt := 0. |
|
328 |
self to:stop do:[:i | |
|
329 |
(aBlock value:i) ifTrue:[ cnt := cnt + 1 ]. |
|
330 |
]. |
|
331 |
^ cnt |
|
332 |
||
333 |
" |
|
334 |
1 to:10 count:[:n | n even] |
|
335 |
" |
|
336 |
! |
|
337 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
338 |
to:stop do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
339 |
"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
|
340 |
evaluate aBlock, passing the number as argument." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
341 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
342 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
343 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
344 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
345 |
[stop < tmp] whileFalse:[ |
6073 | 346 |
aBlock value:tmp. |
347 |
tmp := tmp+1 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
348 |
] |
6073 | 349 |
|
350 |
" |
|
351 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
352 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
353 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
|
354 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
355 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
356 |
|
21806 | 357 |
to:stop do:aBlock separatedBy:actionBetween |
358 |
"For each element of the interval from the receiver up to the argument stop, |
|
359 |
evaluate aBlock, passing the number as argument. |
|
360 |
Between each, evaluate actionBetween (but not before the first and not after the last)" |
|
361 |
||
362 |
|tmp first| |
|
363 |
||
364 |
tmp := self. |
|
365 |
first := true. |
|
366 |
[stop < tmp] whileFalse:[ |
|
367 |
first ifFalse:[ |
|
368 |
actionBetween value. |
|
369 |
]. |
|
370 |
first := false. |
|
371 |
aBlock value:tmp. |
|
372 |
tmp := tmp+1. |
|
373 |
] |
|
374 |
||
375 |
" |
|
376 |
1 to:10 |
|
377 |
do:[:i | Transcript show:i] |
|
378 |
separatedBy:[ Transcript show:', ']. |
|
379 |
" |
|
380 |
||
381 |
"Created: / 14-06-2017 / 15:23:21 / cg" |
|
382 |
! |
|
383 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
384 |
to:stop doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
385 |
"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
|
386 |
evaluate aBlock, passing the number as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
387 |
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
|
388 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
389 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
390 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
391 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
392 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
393 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
394 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
395 |
tmp := tmp+1 |
5565
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 |
|
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 |
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
|
400 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
401 |
index > 5 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
402 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
403 |
]. |
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 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
408 |
to:stop doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
409 |
"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
|
410 |
evaluate aBlock, passing the number as argument. |
15374 | 411 |
Passes an additional exit object, which can be used to leave |
412 |
the loop early, by sending it a #value: message. |
|
413 |
Returns nil or the value passed to the exit>>value: message. |
|
414 |
||
415 |
Notice, that this is different to a return statement in the block, |
|
416 |
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
|
417 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
418 |
|exit tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
419 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
420 |
exit := [:exitValue | ^exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
421 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
422 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
423 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
424 |
tmp := tmp+1 |
15374 | 425 |
]. |
426 |
^ nil |
|
5565
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 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
429 |
1 to:10 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
430 |
index == 5 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
431 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
432 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
433 |
Transcript showCR:index. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
434 |
]. |
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 | 438 |
!Magnitude methodsFor:'testing'! |
439 |
||
440 |
between:min and:max |
|
21838 | 441 |
"return true if the receiver is greater than or equal to the argument min |
18396 | 442 |
and less than or equal to the argument max" |
1 | 443 |
|
444 |
(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
|
445 |
(max < self) ifTrue:[^ false]. |
1 | 446 |
^ true |
56 | 447 |
|
213 | 448 |
" |
449 |
1.2 between:1 and:2 |
|
450 |
(3/2) between:1 and:2 |
|
451 |
(3/2) between:0 and:1 |
|
452 |
" |
|
21838 | 453 |
|
454 |
"Modified (comment): / 19-06-2017 / 14:59:05 / mawalch" |
|
1 | 455 |
! |
456 |
||
457 |
in:anInterval |
|
458 |
"return whether the receiver is within the interval bounds" |
|
459 |
||
460 |
(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
|
461 |
(anInterval stop < self) ifTrue:[^ false]. |
1 | 462 |
^ true |
56 | 463 |
|
213 | 464 |
" |
465 |
1.2 in:(1 to: 2) |
|
466 |
(3/2) in:(1 to: 2) |
|
467 |
(3/2) in:(0 to: 1) |
|
468 |
" |
|
1 | 469 |
! ! |
699 | 470 |
|
471 |
!Magnitude class methodsFor:'documentation'! |
|
472 |
||
473 |
version |
|
19005 | 474 |
^ '$Header$' |
19006 | 475 |
! |
476 |
||
477 |
version_CVS |
|
478 |
^ '$Header$' |
|
699 | 479 |
! ! |
15374 | 480 |