author | Claus Gittinger <cg@exept.de> |
Thu, 15 Mar 2007 16:24:53 +0100 | |
changeset 1856 | a8f74ba03061 |
parent 1758 | e1464deba072 |
child 2145 | 5ceaed7530fe |
permissions | -rw-r--r-- |
62 | 1 |
" |
290 | 2 |
COPYRIGHT. |
3 |
The above file is a Manchester Goodie protected by copyright. |
|
4 |
These conditions are imposed on the whole Goodie, and on any significant |
|
5 |
part of it which is separately transmitted or stored: |
|
355 | 6 |
* You must ensure that every copy includes this notice, and that |
7 |
source and author(s) of the material are acknowledged. |
|
8 |
* These conditions must be imposed on anyone who receives a copy. |
|
9 |
* The material shall not be used for commercial gain without the prior |
|
10 |
written consent of the author(s). |
|
290 | 11 |
Further information on the copyright conditions may be obtained by |
12 |
sending electronic mail: |
|
355 | 13 |
To: goodies-lib@cs.man.ac.uk |
14 |
Subject: copyright |
|
290 | 15 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
16 |
Computer Science, The University, Manchester M13 9PL, UK |
|
62 | 17 |
|
290 | 18 |
(C) Copyright 1992 University of Manchester |
19 |
For more information about the Manchester Goodies Library (from which |
|
20 |
this file was distributed) send e-mail: |
|
355 | 21 |
To: goodies-lib@cs.man.ac.uk |
22 |
Subject: help |
|
290 | 23 |
" |
993 | 24 |
"{ Package: 'stx:libbasic2' }" |
890
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
25 |
|
290 | 26 |
Collection subclass:#Iterator |
27 |
instanceVariableNames:'block' |
|
28 |
classVariableNames:'' |
|
29 |
poolDictionaries:'' |
|
30 |
category:'Collections-Sequenceable' |
|
62 | 31 |
! |
32 |
||
290 | 33 |
Iterator comment:'An Iterator is a read-only collection that evaluates a block to yield the elements |
925 | 34 |
of the collection.' |
35 |
! |
|
62 | 36 |
|
290 | 37 |
!Iterator class methodsFor:'documentation'! |
62 | 38 |
|
290 | 39 |
copyright |
40 |
" |
|
41 |
COPYRIGHT. |
|
62 | 42 |
The above file is a Manchester Goodie protected by copyright. |
43 |
These conditions are imposed on the whole Goodie, and on any significant |
|
44 |
part of it which is separately transmitted or stored: |
|
355 | 45 |
* You must ensure that every copy includes this notice, and that |
46 |
source and author(s) of the material are acknowledged. |
|
47 |
* These conditions must be imposed on anyone who receives a copy. |
|
48 |
* The material shall not be used for commercial gain without the prior |
|
49 |
written consent of the author(s). |
|
62 | 50 |
Further information on the copyright conditions may be obtained by |
51 |
sending electronic mail: |
|
355 | 52 |
To: goodies-lib@cs.man.ac.uk |
53 |
Subject: copyright |
|
62 | 54 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
55 |
Computer Science, The University, Manchester M13 9PL, UK |
|
56 |
||
57 |
(C) Copyright 1992 University of Manchester |
|
58 |
For more information about the Manchester Goodies Library (from which |
|
59 |
this file was distributed) send e-mail: |
|
355 | 60 |
To: goodies-lib@cs.man.ac.uk |
61 |
Subject: help |
|
290 | 62 |
" |
63 |
||
64 |
! |
|
65 |
||
66 |
documentation |
|
67 |
" |
|
68 |
Occasionally you may have a block that when evaluated can be |
|
355 | 69 |
treated as a collection -- i.e. it takes another block as parameter, |
290 | 70 |
then applies that to a sequence of values. |
71 |
||
72 |
This goodie wraps the block into an object -- an iterator -- which is |
|
73 |
part of the collection hierarchy, and therefore inherits a variety of |
|
74 |
useful collection-related methods. |
|
75 |
||
76 |
[info:] |
|
355 | 77 |
NAME Iterator |
78 |
AUTHOR miw@cs.man.ac.uk (Mario Wolczko) |
|
79 |
FUNCTION a wrapper for blocks that iterate over collections |
|
80 |
ST-VERSION 4.0 4.1 |
|
81 |
PREREQUISITES |
|
82 |
CONFLICTS |
|
83 |
DISTRIBUTION world |
|
84 |
VERSION 1 |
|
85 |
DATE 18 Jun 1991 |
|
86 |
SUMMARY |
|
290 | 87 |
|
88 |
[organisation:] |
|
355 | 89 |
Dept. of Computer Science Internet: mario@cs.man.ac.uk |
90 |
The University uucp: uknet!!!!man.cs!!!!mario |
|
91 |
Manchester M13 9PL JANET: mario@uk.ac.man.cs |
|
92 |
U.K. Tel: +44-61-275 6146 (FAX: 6236) |
|
93 |
______the mushroom project___________________________________ |
|
290 | 94 |
|
95 |
[author:] |
|
355 | 96 |
Mario Wolczko miw@cs.man.ac.uk |
290 | 97 |
" |
98 |
||
99 |
! |
|
100 |
||
101 |
examples |
|
102 |
" |
|
878 | 103 |
an iterator, simulating the collection of all classes in the system: |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
104 |
[exBegin] |
878 | 105 |
|i b| |
106 |
||
107 |
b := [:whatToDo | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
108 |
Smalltalk allClassesDo:[:cls | |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
109 |
whatToDo value:cls |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
110 |
] |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
111 |
]. |
878 | 112 |
|
113 |
i := Iterator on:b. |
|
114 |
i do:[:cls | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
115 |
Transcript showCR:cls name |
878 | 116 |
]. |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
117 |
[exEnd] |
878 | 118 |
much like above, one that simulates the collection of all methodNames starting with 'a': |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
119 |
[exBegin] |
290 | 120 |
|i b| |
121 |
||
122 |
b := [:whatToDo | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
123 |
Smalltalk allClassesDo:[:cls | |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
124 |
cls methodDictionary keysAndValuesDo:[:nm :mthd | |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
125 |
(nm startsWith:$a) ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
126 |
whatToDo value:nm |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
127 |
] |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
128 |
] |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
129 |
] |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
130 |
]. |
290 | 131 |
|
878 | 132 |
i := Iterator on:b. |
133 |
i do:[:nm | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
134 |
Transcript showCR:nm |
878 | 135 |
]. |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
136 |
[exEnd] |
878 | 137 |
an iterator, simulating a collection of 100 random values: |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
138 |
[exBegin] |
878 | 139 |
|i b| |
140 |
||
141 |
b := [:whatToDo | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
142 |
|rnd| |
878 | 143 |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
144 |
rnd := Random new. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
145 |
1 to:100 do:[:i | |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
146 |
whatToDo value:(rnd next) |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
147 |
] |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
148 |
]. |
290 | 149 |
|
150 |
i := Iterator on:b. |
|
151 |
i do:[:j | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
152 |
j printNL |
290 | 153 |
]. |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
154 |
[exEnd] |
294 | 155 |
an iterator, simulating a collection of the lines |
156 |
in a file: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
157 |
[exBegin] |
294 | 158 |
|i b| |
159 |
||
160 |
b := [:whatToDo | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
161 |
|s line| |
294 | 162 |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
163 |
s := 'smalltalk.rc' asFilename readStream. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
164 |
[s atEnd] whileFalse:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
165 |
line := s nextLine. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
166 |
whatToDo value:line. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
167 |
]. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
168 |
s close |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
169 |
]. |
294 | 170 |
|
171 |
i := Iterator on:b. |
|
172 |
i do:[:j | |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
173 |
j printNL |
294 | 174 |
]. |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
878
diff
changeset
|
175 |
[exEnd] |
290 | 176 |
" |
177 |
! ! |
|
178 |
||
179 |
!Iterator class methodsFor:'instance creation'! |
|
180 |
||
181 |
on: aBlock |
|
182 |
^ self new block: aBlock |
|
183 |
||
184 |
" |
|
185 |
|i b| |
|
186 |
||
187 |
b := [:whatToDo | |
|
355 | 188 |
1 to:10 do:[:i | |
189 |
whatToDo value:i |
|
190 |
] |
|
191 |
]. |
|
290 | 192 |
|
193 |
i := Iterator on:b. |
|
194 |
i do:[:j | |
|
355 | 195 |
j printNL |
290 | 196 |
]. |
197 |
" |
|
198 |
||
199 |
" |
|
200 |
an iterator, simulating a collection of 100 random values: |
|
201 |
||
202 |
|i b| |
|
203 |
||
204 |
b := [:whatToDo | |
|
355 | 205 |
|rnd| |
290 | 206 |
|
355 | 207 |
rnd := Random new. |
208 |
1 to:100 do:[:i | |
|
209 |
whatToDo value:(rnd next) |
|
210 |
] |
|
211 |
]. |
|
290 | 212 |
|
213 |
i := Iterator on:b. |
|
214 |
i do:[:j | |
|
355 | 215 |
j printNL |
290 | 216 |
]. |
217 |
" |
|
218 |
||
219 |
"Modified: 9.5.1996 / 14:26:49 / cg" |
|
220 |
! |
|
221 |
||
222 |
on:collection msg:msg |
|
223 |
^ self new block: [ :aBlock | collection perform: msg with: aBlock] |
|
224 |
||
225 |
" |
|
226 |
|rnd i| |
|
227 |
||
228 |
rnd := Random new. |
|
229 |
i := Iterator on:[:a | rnd next]. |
|
230 |
i do:[:j | |
|
355 | 231 |
j printNL |
290 | 232 |
]. |
233 |
" |
|
234 |
||
235 |
"Modified: 9.5.1996 / 14:21:40 / cg" |
|
236 |
! ! |
|
237 |
||
238 |
!Iterator methodsFor:'accessing'! |
|
239 |
||
240 |
identityIndexOf: anElement |
|
241 |
"Answer the identity index of anElement within the receiver. |
|
242 |
If the receiver does not contain anElement, answer 0." |
|
243 |
||
244 |
^self identityIndexOf: anElement ifAbsent: [0] |
|
245 |
! |
|
246 |
||
247 |
identityIndexOf: anElement ifAbsent: exceptionBlock |
|
248 |
"Answer the identity index of anElement within the receiver. |
|
249 |
If the receiver does not contain anElement, answer the result |
|
250 |
of evaluating the exceptionBlock." |
|
251 |
||
252 |
| index | |
|
253 |
index := 1. |
|
254 |
self do: [ :el | el == anElement ifTrue: [^index]. index := index + 1]. |
|
255 |
^exceptionBlock value |
|
256 |
! |
|
257 |
||
258 |
indexOf: anElement |
|
259 |
"Answer the index of anElement within the receiver. If the receiver does |
|
260 |
not contain anElement, answer 0." |
|
261 |
||
262 |
^self indexOf: anElement ifAbsent: [0] |
|
263 |
! |
|
264 |
||
265 |
indexOf: anElement ifAbsent: exceptionBlock |
|
266 |
"Answer the index of anElement within the receiver. If the receiver does |
|
267 |
not contain anElement, answer the result of evaluating the exceptionBlock." |
|
268 |
||
269 |
||
270 |
| index | |
|
271 |
index := 1. |
|
272 |
self do: [ :el | el = anElement ifTrue: [^index]. index := index + 1]. |
|
273 |
^exceptionBlock value |
|
274 |
! ! |
|
275 |
||
276 |
!Iterator methodsFor:'adding & removing'! |
|
277 |
||
278 |
add: anObject |
|
279 |
"Iterators are read-only" |
|
280 |
self shouldNotImplement |
|
281 |
! |
|
282 |
||
283 |
remove: oldObject ifAbsent: anExceptionBlock |
|
284 |
"Iterators are read-only." |
|
285 |
self shouldNotImplement |
|
897 | 286 |
! |
287 |
||
288 |
removeIdentical: oldObject ifAbsent: anExceptionBlock |
|
289 |
"Iterators are read-only." |
|
290 |
self shouldNotImplement |
|
290 | 291 |
! ! |
292 |
||
293 |
!Iterator methodsFor:'converting'! |
|
294 |
||
295 |
asOrderedCollection |
|
296 |
"Answer a new instance of OrderedCollection whose elements are the elements of |
|
297 |
the receiver. The order in which elements are added depends on the order in |
|
298 |
which the receiver enumerates its elements. In the case of unordered collections, |
|
299 |
the ordering is not necessarily the same for multiple requests for the conversion." |
|
300 |
||
301 |
||
302 |
| anOrderedCollection | |
|
303 |
anOrderedCollection := OrderedCollection new. |
|
304 |
self do: [:each | anOrderedCollection addLast: each]. |
|
305 |
^anOrderedCollection |
|
306 |
! ! |
|
307 |
||
308 |
!Iterator methodsFor:'enumerating'! |
|
309 |
||
310 |
do: aBlock |
|
311 |
block value: aBlock |
|
312 |
! |
|
313 |
||
314 |
findFirst: aBlock |
|
315 |
"Answer the index of the first element of the receiver |
|
316 |
for which aBlock evaluates as true." |
|
317 |
||
318 |
| index | |
|
319 |
index := 1. |
|
320 |
self do: [ :el | (aBlock value: el) ifTrue: [^index]. index := index + 1]. |
|
321 |
||
322 |
^0 |
|
323 |
! |
|
324 |
||
325 |
findLast: aBlock |
|
326 |
"Answer the index of the last element of the receiver |
|
1425 | 327 |
for which aBlock evaluates as true." |
290 | 328 |
|
329 |
| index last | |
|
330 |
index := 1. |
|
331 |
last := 0. |
|
332 |
self do: [ :el | (aBlock value: el) ifTrue: [last := index]. index := index + 1]. |
|
333 |
^last |
|
334 |
! |
|
335 |
||
336 |
keysAndValuesDo: aBlock |
|
337 |
"Evaluate aBlock with each of the receiver's key/value pairs |
|
338 |
(e.g. indexes and elements) as the arguments." |
|
339 |
||
340 |
| index | |
|
341 |
index := 1. |
|
342 |
self do: [:el | aBlock value: index value: el. index := index + 1] |
|
343 |
! ! |
|
344 |
||
890
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
345 |
!Iterator methodsFor:'printing & storing'! |
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
346 |
|
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
347 |
displayString |
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
348 |
^ 'Iterator(' , block displayString , ')' |
1756 | 349 |
|
1758 | 350 |
"Modified: / 06-10-2006 / 14:35:23 / cg" |
1755 | 351 |
! |
352 |
||
353 |
printOn:aStream |
|
354 |
aStream nextPutAll:'Iterator('. |
|
355 |
block printOn:aStream. |
|
356 |
aStream nextPutAll:')'. |
|
357 |
||
358 |
"Created: / 06-10-2006 / 13:44:42 / cg" |
|
890
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
359 |
! ! |
40421d1c3377
displayString (avoid iterating)
Claus Gittinger <cg@exept.de>
parents:
885
diff
changeset
|
360 |
|
290 | 361 |
!Iterator methodsFor:'private'! |
362 |
||
925 | 363 |
block |
364 |
^ block |
|
365 |
! |
|
366 |
||
290 | 367 |
block: aBlock |
368 |
block := aBlock |
|
369 |
! |
|
370 |
||
371 |
species |
|
1036 | 372 |
"return the type of collection to be returned by collect, select etc." |
373 |
||
374 |
^ OrderedCollection |
|
290 | 375 |
! ! |
376 |
||
586 | 377 |
!Iterator methodsFor:'queries'! |
378 |
||
1856 | 379 |
isEmpty |
380 |
^ false |
|
381 |
||
382 |
"Created: / 15-03-2007 / 16:23:48 / cg" |
|
383 |
! |
|
384 |
||
586 | 385 |
size |
386 |
self shouldNotImplement |
|
387 |
||
388 |
"Created: / 1.11.1997 / 17:29:03 / cg" |
|
389 |
! ! |
|
390 |
||
290 | 391 |
!Iterator class methodsFor:'documentation'! |
392 |
||
393 |
version |
|
1856 | 394 |
^ '$Header: /cvs/stx/stx/libbasic2/Iterator.st,v 1.19 2007-03-15 15:24:53 cg Exp $' |
290 | 395 |
! ! |