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