@@ -24,9 +24,9 @@ trait Collection:
24
24
*/
25
25
def count : Int =
26
26
val e = endPosition
27
- def _count (p : Position , n : Int ): Int =
28
- if p `eq` e then n else _count (self.positionAfter(p), n + 1 )
29
- _count (startPosition, 0 )
27
+ def loop (p : Position , n : Int ): Int =
28
+ if p `eq` e then n else loop (self.positionAfter(p), n + 1 )
29
+ loop (startPosition, 0 )
30
30
31
31
/** Returns the position of `self`'s first element', or `endPosition` if `self` is empty.
32
32
*
@@ -80,7 +80,7 @@ trait Collection:
80
80
recur(self.positionAfter(i))
81
81
end Collection
82
82
83
- extension [Self : Collection ](self : Self ) {
83
+ extension [Self : Collection ](self : Self )
84
84
85
85
/** Returns the first element of `self` along with a slice containing the suffix after this
86
86
* element, or `None` if `self` is empty.
@@ -89,28 +89,24 @@ extension [Self: Collection](self: Self) {
89
89
* O(1)
90
90
*/
91
91
def headAndTail : Option [(Self .Element , Slice [Self ])] =
92
- if ( self.isEmpty) {
92
+ if self.isEmpty then
93
93
None
94
- } else {
94
+ else
95
95
val p = self.startPosition
96
96
val q = self.positionAfter(p)
97
97
val t = Slice (self, Range (q, self.endPosition, (a, b) => (a `eq` b) || self.isBefore(a, b)))
98
98
Some ((self.at(p), t))
99
- }
100
99
101
100
/** Applies `combine` on `partialResult` and each element of `self`, in order.
102
101
*
103
102
* @complexity
104
103
* O(n) where n is the number of elements in `self`.
105
104
*/
106
- def reduce [T ](partialResult : T , combine : (T , Self .Element ) => T ): T =
105
+ def reduce [T ](partialResult : T )( combine : (T , Self .Element ) => T ): T =
107
106
val e = self.endPosition
108
107
def loop (p : Self .Position , r : T ): T =
109
- if (p.eq(e)) {
110
- r
111
- } else {
112
- loop(self.positionAfter(p), combine(r, self.at(p)))
113
- }
108
+ if p `eq` e then r
109
+ else loop(self.positionAfter(p), combine(r, self.at(p)))
114
110
loop(self.startPosition, partialResult)
115
111
116
112
/** Applies `action` on each element of `self`, in order, until `action` returns `false`, and
@@ -122,54 +118,46 @@ extension [Self: Collection](self: Self) {
122
118
* @complexity
123
119
* O(n) where n is the number of elements in `self`.
124
120
*/
125
- def forEach (action : ( Self .Element ) => Boolean ): Boolean =
121
+ def forEach (action : Self .Element => Boolean ): Boolean =
126
122
val e = self.endPosition
127
123
def loop (p : Self .Position ): Boolean =
128
- if (p.eq(e)) {
129
- true
130
- } else if (! action(self.at(p))) {
131
- false
132
- } else {
133
- loop(self.positionAfter(p))
134
- }
124
+ if p `eq` e then true
125
+ else if ! action(self.at(p)) then false
126
+ else loop(self.positionAfter(p))
135
127
loop(self.startPosition)
136
128
137
129
/** Returns a collection with the elements of `self` transformed by `transform`, in order.
138
130
*
139
131
* @complexity
140
132
* O(n) where n is the number of elements in `self`.
141
133
*/
142
- def map [T : Value ](transform : (Self .Element ) => T ): HyArray [T ] =
143
- self.reduce(
144
- HyArray [T ](),
145
- (r, e) => r.append(transform(e), assumeUniqueness = true )
146
- )
134
+ def map [T : Value ](transform : Self .Element => T ): HyArray [T ] =
135
+ self.reduce(HyArray [T ]()): (r, e) =>
136
+ r.append(transform(e), assumeUniqueness = true )
147
137
148
138
/** Returns a collection with the elements of `self` satisfying `isInclude`, in order.
149
139
*
150
140
* @complexity
151
141
* O(n) where n is the number of elements in `self`.
152
142
*/
153
- def filter (isIncluded : (Self .Element ) => Boolean ): HyArray [Self .Element ] =
154
- self.reduce(
155
- HyArray [Self .Element ](),
156
- (r, e) => if (isIncluded(e)) then r.append(e, assumeUniqueness = true ) else r
157
- )
143
+ def filter (isIncluded : Self .Element => Boolean ): HyArray [Self .Element ] =
144
+ self.reduce(HyArray [Self .Element ]()): (r, e) =>
145
+ if isIncluded(e) then r.append(e, assumeUniqueness = true ) else r
158
146
159
147
/** Returns `true` if `self` contains an element satisfying `predicate`.
160
148
*
161
149
* @complexity
162
150
* O(n) where n is the number of elements in `self`.
163
151
*/
164
- def containsWhere (predicate : ( Self .Element ) => Boolean ): Boolean =
152
+ def containsWhere (predicate : Self .Element => Boolean ): Boolean =
165
153
self.firstPositionWhere(predicate) != None
166
154
167
155
/** Returns `true` if all elements in `self` satisfy `predicate`.
168
156
*
169
157
* @complexity
170
158
* O(n) where n is the number of elements in `self`.
171
159
*/
172
- def allSatisfy (predicate : ( Self .Element ) => Boolean ): Boolean =
160
+ def allSatisfy (predicate : Self .Element => Boolean ): Boolean =
173
161
self.firstPositionWhere(predicate) == None
174
162
175
163
/** Returns the position of the first element of `self` satisfying `predicate`, or `None` if no
@@ -178,16 +166,12 @@ extension [Self: Collection](self: Self) {
178
166
* @complexity
179
167
* O(n) where n is the number of elements in `self`.
180
168
*/
181
- def firstPositionWhere (predicate : ( Self .Element ) => Boolean ): Option [Self .Position ] =
169
+ def firstPositionWhere (predicate : Self .Element => Boolean ): Option [Self .Position ] =
182
170
val e = self.endPosition
183
171
def loop (p : Self .Position ): Option [Self .Position ] =
184
- if (p.eq(e)) {
185
- None
186
- } else if (predicate(self.at(p))) {
187
- Some (p)
188
- } else {
189
- loop(self.positionAfter(p))
190
- }
172
+ if p `eq` e then None
173
+ else if predicate(self.at(p)) then Some (p)
174
+ else loop(self.positionAfter(p))
191
175
loop(self.startPosition)
192
176
193
177
/** Returns the minimum element in `self`, using `isLessThan` to compare elements.
@@ -229,26 +213,19 @@ extension [Self: Collection](self: Self) {
229
213
* O(n) where n is the number of elements in `self`.
230
214
*/
231
215
def leastElement (isOrderedBefore : (Self .Element , Self .Element ) => Boolean ): Option [Self .Element ] =
232
- if ( self.isEmpty) {
216
+ if self.isEmpty then
233
217
None
234
- } else {
218
+ else
235
219
val e = self.endPosition
236
- def _least (p : Self .Position , least : Self .Element ): Self .Element =
237
- if (p.eq(e)) {
220
+ def loop (p : Self .Position , least : Self .Element ): Self .Element =
221
+ if p `eq` e then
238
222
least
239
- } else {
223
+ else
240
224
val x = self.at(p)
241
225
val y = if isOrderedBefore(x, least) then x else least
242
- _least(self.positionAfter(p), y)
243
- }
244
-
226
+ loop(self.positionAfter(p), y)
245
227
val b = self.startPosition
246
- Some (_least(self.positionAfter(b), self.at(b)))
247
- }
248
-
249
- }
250
-
251
- extension [Self : Collection ](self : Self )
228
+ Some (loop(self.positionAfter(b), self.at(b)))
252
229
253
230
/** Returns `true` if `self` contains the same elements as `other`, in the same order. */
254
231
def elementsEqual [T : Collection { type Element = Self .Element } ](other : T ): Boolean =
@@ -262,4 +239,4 @@ extension [Self: Collection](self: Self)
262
239
else
263
240
loop(self.positionAfter(i), other.positionAfter(j))
264
241
loop(self.startPosition, other.startPosition)
265
-
242
+ end extension
0 commit comments