/
IteratorWithCollectionAPI.class.st
313 lines (260 loc) · 8.87 KB
/
IteratorWithCollectionAPI.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"
I am an iterator decorator that provides an API similar to Collection's API.
"
Class {
#name : #IteratorWithCollectionAPI,
#superclass : #IteratorDecorator,
#category : #'Iterators-Decorators-Decorators'
}
{ #category : #accessing }
IteratorWithCollectionAPI >> allButFirst [
"Answer an array containing all but the first element.
Raise an error if there are not enough elements."
^ self allButFirst: 1
]
{ #category : #accessing }
IteratorWithCollectionAPI >> allButFirst: n [
"Answer an array containing all but the first n elements.
Raise an error if there are not enough elements."
^ (self decoratedBy: n skipIt) upToEnd
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> allSatisfy: aBlock [
"Evaluate aBlock with the nodes of the receiver.
If aBlock returns false for any element return false.
Otherwise return true."
self do: [:each | (aBlock value: each) ifFalse: [^ false]].
^ true
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> anySatisfy: aBlock [
"Evaluate aBlock with the nodes of the receiver.
If aBlock returns true for any element return true.
Otherwise return false."
self do: [:each | (aBlock value: each) ifTrue: [^ true]].
^ false
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> collect: aBlock [
^ self collect: aBlock as: Array
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> collect: aBlock as: aSequenceableCollection [
"Evaluate aBlock with each of the receiver's elements as the argument.
Collect the resulting values into an instance of aSequenceableCollection.
Answer the new collection."
^ aSequenceableCollection streamContents: [ :stream |
self do: [ :each | stream nextPut: (aBlock value: each)] ].
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> count: aBlock [
"Evaluate aBlock with each of the receiver's nodes as the argument.
Answer the number of nodes that answered true."
| sum |
sum := 0.
self do: [:each | (aBlock value: each) ifTrue: [sum := sum + 1]].
^ sum
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> detect: aBlock [
^ self detect: aBlock ifNone: [ NotFound signalFor: aBlock ]
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> detect: aBlock ifFound: ifFoundBlock [
self detect: aBlock ifFound: ifFoundBlock ifNone: [ "Do nothing on purpose" ]
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> detect: aBlock ifFound: ifFoundBlock ifNone: ifNoneBlock [
"Search for the first node for which aBlock returns true.
If found, evaluate ifFoundBlock with the found node as arg.
If not found, evaluate ifNoneBlock without arg."
self do: [ :child |
(aBlock value: child)
ifTrue: [ ^ ifFoundBlock cull: child ] ].
^ ifNoneBlock value
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> detect: aBlock ifNone: ifNoneBlock [
^ self detect: aBlock ifFound: [ :found | found ] ifNone: ifNoneBlock
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> detectMax: aBlock [
"Evaluate aBlock with node of the receiver's nodes as the argument.
Answer the element for which aBlock evaluates to the highest magnitude.
If collection empty, return nil."
| maxElement maxValue |
maxValue := nil.
self do: [:each |
| val |
maxValue
ifNotNil: [
(val := aBlock value: each) > maxValue ifTrue: [
maxElement := each.
maxValue := val]]
ifNil: ["first element"
maxElement := each.
maxValue := aBlock value: each ] ].
^ maxElement
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> detectMin: aBlock [
"Evaluate aBlock with each of the receiver's nodes as the argument.
Answer the element for which aBlock evaluates to the lowest number.
If collection empty, return nil."
| minElement minValue |
minValue := nil.
self do: [:each |
| val |
minValue
ifNotNil: [
(val := aBlock value: each) < minValue ifTrue: [
minElement := each.
minValue := val]]
ifNil: ["first element"
minElement := each.
minValue := aBlock value: each ] ].
^ minElement
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> do: aBlock [
"Executes aBlock on all the objects returned by #next until #hasNextReturn false."
[ self hasNext ] whileTrue: [
aBlock value: self next ]
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> do: doBlock separatedBy: separatorBlock [
"Evaluate the doBlock for all nodes in the receiver,
and evaluate the separatorBlock between."
| beforeFirst |
beforeFirst := true.
self do:
[:each |
beforeFirst
ifTrue: [beforeFirst := false]
ifFalse: [separatorBlock value].
doBlock value: each ]
]
{ #category : #testing }
IteratorWithCollectionAPI >> identityIncludes: anObject [
"Answer whether anObject is one of the receiver's elements."
self do: [:each | anObject == each ifTrue: [^true]].
^false
]
{ #category : #testing }
IteratorWithCollectionAPI >> ifEmpty: aBlock [
"Evaluate the given block, answering its value if the receiver is empty, otherwise answer the receiver."
^ self isEmpty
ifTrue: [ aBlock value ]
ifFalse: [ self ]
]
{ #category : #testing }
IteratorWithCollectionAPI >> ifEmpty: emptyBlock ifNotEmpty: notEmptyBlock [
"Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise"
"If the notEmptyBlock has an argument, eval with the receiver as its argument"
self isEmpty ifTrue: [ ^emptyBlock value ].
^notEmptyBlock cull: self
]
{ #category : #testing }
IteratorWithCollectionAPI >> ifNotEmpty: aBlock [
"Evaluate the given block with the receiver as argument, answering its value
unless the receiver is empty, in which case answer the receiver."
^self isEmpty
ifTrue: [self]
ifFalse: [aBlock cull: self]
]
{ #category : #testing }
IteratorWithCollectionAPI >> ifNotEmpty: notEmptyBlock ifEmpty: emptyBlock [
"Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise
If the notEmptyBlock has an argument, eval with the receiver as its argument"
self isEmpty ifFalse: [ ^notEmptyBlock cull: self ].
^emptyBlock value
]
{ #category : #testing }
IteratorWithCollectionAPI >> includes: anObject [
"Answer whether anObject is one of the receiver's elements."
^ self anySatisfy: [:each | each = anObject]
]
{ #category : #testing }
IteratorWithCollectionAPI >> includesAll: aCollection [
"Answer whether all the elements of aCollection are in the receiver."
self flag: #TODO;error. "Can not be implemented as done below."
aCollection do: [:elem | (self includes: elem) ifFalse: [^ false]].
^ true
]
{ #category : #testing }
IteratorWithCollectionAPI >> includesAny: aCollection [
"Answer whether any element of aCollection is one of the receiver's elements."
aCollection do: [:elem | (self includes: elem) ifTrue: [^ true]].
^ false
]
{ #category : #testing }
IteratorWithCollectionAPI >> isEmpty [
^ self atEnd
]
{ #category : #testing }
IteratorWithCollectionAPI >> isNotEmpty [
^ self hasNext
]
{ #category : #accessing }
IteratorWithCollectionAPI >> iterator [
"To be polymorphic with objects providing an iterator.
No need to decorate twice.
"
^ self
]
{ #category : #testing }
IteratorWithCollectionAPI >> occurrencesOf: anObject [
"Answer how many of the receiver's elements are equal to anObject."
| tally |
tally := 0.
self do: [:each | anObject = each ifTrue: [tally := tally + 1]].
^tally
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> reject: aBlock [
^ self reject: aBlock as: Array
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> reject: aBlock as: aSequenceableCollectionClass [
"Selects nodes of the AST for which aBlock returns false.
Returns these nodes in aSequenceableCollectionClass instance."
^ aSequenceableCollectionClass streamContents: [ :stream |
self do: [ :child |
(aBlock value: child)
ifFalse: [ stream nextPut: child ] ] ]
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> select: aBlock [
^ self select: aBlock as: Array
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> select: aBlock as: aSequenceableCollectionClass [
"Select nodes of the AST for which aBlock returns true.
Returns these nodes in aSequenceableCollectionClass instance."
^ aSequenceableCollectionClass streamContents: [ :stream |
self do: [ :child |
(aBlock value: child)
ifTrue: [ stream nextPut: child ] ] ]
]
{ #category : #sorting }
IteratorWithCollectionAPI >> sorted [
^ self sorted: [ :a :b| a <= b ]
]
{ #category : #sorting }
IteratorWithCollectionAPI >> sorted: aSortBlockOrNil [
"Return an array which contains the same elements as self but its
elements are sorted by aSortBlockOrNil. The block should take two arguments and return true if
the first element should preceed the second one. If aSortBlock is nil then <= is used for
comparison."
^self upToEnd sort: aSortBlockOrNil
]
{ #category : #enumerating }
IteratorWithCollectionAPI >> sum: aBlock [
"Evaluate aBlock with each of the receiver's nodes as the argument.
Return the sum of the answers."
| sum |
sum := 0.
self do: [:each |
sum := (aBlock value: each) + sum].
^ sum
]