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