-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterator.class.st
More file actions
156 lines (131 loc) · 3.31 KB
/
Copy pathIterator.class.st
File metadata and controls
156 lines (131 loc) · 3.31 KB
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
"
An Iterator is a read-only collection that evaluates a block to yield the elements of the collection.
"
Class {
#name : #Iterator,
#superclass : #Collection,
#instVars : [
'block'
],
#category : #'Mathematics-Kernel-Support'
}
{ #category : #'instance creation' }
Iterator class >> on: aBlock [
^ self new block: aBlock
]
{ #category : #'instance creation' }
Iterator class >> on: anObject performing: aSymbol [
^ self new
block: [ :aBlock | anObject perform: aSymbol with: aBlock ]
]
{ #category : #adding }
Iterator >> add: anObject [
"Iterators are read-only"
self shouldNotImplement
]
{ #category : #private }
Iterator >> block: aBlock [
block := aBlock
]
{ #category : #enumerating }
Iterator >> do: aBlock [
block value: aBlock
]
{ #category : #enumerating }
Iterator >> findFirst: aBlock [
"Answer the index of the first element of the receiver
for which aBlock evaluates as true."
| index |
index := 1.
self
do: [ :el |
(aBlock value: el)
ifTrue: [ ^ index ].
index := index + 1 ].
^ 0
]
{ #category : #enumerating }
Iterator >> findLast: aBlock [
"Answer the index of the last element of the receiver
for which aBlock evaluates as true."
| index last |
index := 1.
last := 0.
self
do: [ :el |
(aBlock value: el)
ifTrue: [ last := index ].
index := index + 1 ].
^ last
]
{ #category : #copying }
Iterator >> first: n [
"Answer the first n elements of the receiver.
Raise an error if there are not enough elements."
| answer |
answer := self species new: n.
self
do: [ :each |
answer size = n
ifTrue: [ ^ answer ].
answer add: each ].
^ self error: 'not enough elements'
]
{ #category : #accessing }
Iterator >> identityIndexOf: anElement [
"Answer the identity index of anElement within the receiver. If the receiver does
not contain anElement, answer 0."
^ self identityIndexOf: anElement ifAbsent: [ 0 ]
]
{ #category : #accessing }
Iterator >> identityIndexOf: anElement ifAbsent: exceptionBlock [
"Answer the identity index of anElement within the receiver. If the receiver does
not contain anElement, answer the result of evaluating the exceptionBlock."
| index |
index := 1.
self
do: [ :el |
el == anElement
ifTrue: [ ^ index ].
index := index + 1 ].
^ exceptionBlock value
]
{ #category : #accessing }
Iterator >> indexOf: anElement [
"Answer the index of anElement within the receiver. If the receiver does
not contain anElement, answer 0."
^ self indexOf: anElement ifAbsent: [ 0 ]
]
{ #category : #accessing }
Iterator >> indexOf: anElement ifAbsent: exceptionBlock [
"Answer the index of anElement within the receiver. If the receiver does
not contain anElement, answer the result of evaluating the exceptionBlock."
| index |
index := 1.
self
do: [ :el |
el = anElement
ifTrue: [ ^ index ].
index := index + 1 ].
^ exceptionBlock value
]
{ #category : #enumerating }
Iterator >> keysAndValuesDo: aBlock [
"Evaluate aBlock with each of the receiver's key/value pairs
(e.g. indexes and elements) as the arguments."
| index |
index := 1.
self
do: [ :el |
aBlock value: index value: el.
index := index + 1 ]
]
{ #category : #removing }
Iterator >> remove: oldObject ifAbsent: anExceptionBlock [
"Iterators are read-only."
self shouldNotImplement
]
{ #category : #private }
Iterator >> species [
^ OrderedCollection
]