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