2
2
package hylo
3
3
4
4
/** A collection of elements accessible by their position. */
5
- trait Collection {
5
+ trait Collection :
6
6
type Self
7
7
8
8
/** The type of the elements in the collection. */
9
9
type Element : Value
10
10
11
11
/** The type of a position in the collection. */
12
- type Position : Value as positionIsValue
12
+ type Position : Value
13
13
14
- extension (self : Self ) {
14
+ extension (self : Self )
15
15
16
16
/** Returns `true` iff `self` is empty. */
17
17
def isEmpty : Boolean =
@@ -70,35 +70,25 @@ trait Collection {
70
70
*/
71
71
def isBefore (i : Position , j : Position ): Boolean =
72
72
val e = self.endPosition
73
- if (i.eq(e)) {
74
- false
75
- } else if (j.eq(e)) {
76
- true
77
- } else {
78
- def _isBefore (n : Position ): Boolean =
79
- if (n.eq(j)) {
80
- true
81
- } else if (n.eq(e)) {
82
- false
83
- } else {
84
- _isBefore(self.positionAfter(n))
85
- }
86
- _isBefore(self.positionAfter(i))
87
- }
88
-
89
- }
90
-
91
- }
92
-
93
- extension [Self : Collection as s ](self : Self ) {
73
+ if i `eq` e then false
74
+ else if j `eq` e then true
75
+ else
76
+ def recur (n : Position ): Boolean =
77
+ if n `eq` j then true
78
+ else if n `eq` e then false
79
+ else recur(self.positionAfter(n))
80
+ recur(self.positionAfter(i))
81
+ end Collection
82
+
83
+ extension [Self : Collection ](self : Self ) {
94
84
95
85
/** Returns the first element of `self` along with a slice containing the suffix after this
96
86
* element, or `None` if `self` is empty.
97
87
*
98
88
* @complexity
99
89
* O(1)
100
90
*/
101
- def headAndTail : Option [(s .Element , Slice [Self ])] =
91
+ def headAndTail : Option [(Self .Element , Slice [Self ])] =
102
92
if (self.isEmpty) {
103
93
None
104
94
} else {
@@ -113,9 +103,9 @@ extension [Self: Collection as s](self: Self) {
113
103
* @complexity
114
104
* O(n) where n is the number of elements in `self`.
115
105
*/
116
- def reduce [T ](partialResult : T , combine : (T , s .Element ) => T ): T =
106
+ def reduce [T ](partialResult : T , combine : (T , Self .Element ) => T ): T =
117
107
val e = self.endPosition
118
- def loop (p : s .Position , r : T ): T =
108
+ def loop (p : Self .Position , r : T ): T =
119
109
if (p.eq(e)) {
120
110
r
121
111
} else {
@@ -132,9 +122,9 @@ extension [Self: Collection as s](self: Self) {
132
122
* @complexity
133
123
* O(n) where n is the number of elements in `self`.
134
124
*/
135
- def forEach (action : (s .Element ) => Boolean ): Boolean =
125
+ def forEach (action : (Self .Element ) => Boolean ): Boolean =
136
126
val e = self.endPosition
137
- def loop (p : s .Position ): Boolean =
127
+ def loop (p : Self .Position ): Boolean =
138
128
if (p.eq(e)) {
139
129
true
140
130
} else if (! action(self.at(p))) {
@@ -149,7 +139,7 @@ extension [Self: Collection as s](self: Self) {
149
139
* @complexity
150
140
* O(n) where n is the number of elements in `self`.
151
141
*/
152
- def map [T : Value ](transform : (s .Element ) => T ): HyArray [T ] =
142
+ def map [T : Value ](transform : (Self .Element ) => T ): HyArray [T ] =
153
143
self.reduce(
154
144
HyArray [T ](),
155
145
(r, e) => r.append(transform(e), assumeUniqueness = true )
@@ -160,9 +150,9 @@ extension [Self: Collection as s](self: Self) {
160
150
* @complexity
161
151
* O(n) where n is the number of elements in `self`.
162
152
*/
163
- def filter (isIncluded : (s .Element ) => Boolean ): HyArray [s .Element ] =
153
+ def filter (isIncluded : (Self .Element ) => Boolean ): HyArray [Self .Element ] =
164
154
self.reduce(
165
- HyArray [s .Element ](),
155
+ HyArray [Self .Element ](),
166
156
(r, e) => if (isIncluded(e)) then r.append(e, assumeUniqueness = true ) else r
167
157
)
168
158
@@ -171,15 +161,15 @@ extension [Self: Collection as s](self: Self) {
171
161
* @complexity
172
162
* O(n) where n is the number of elements in `self`.
173
163
*/
174
- def containsWhere (predicate : (s .Element ) => Boolean ): Boolean =
164
+ def containsWhere (predicate : (Self .Element ) => Boolean ): Boolean =
175
165
self.firstPositionWhere(predicate) != None
176
166
177
167
/** Returns `true` if all elements in `self` satisfy `predicate`.
178
168
*
179
169
* @complexity
180
170
* O(n) where n is the number of elements in `self`.
181
171
*/
182
- def allSatisfy (predicate : (s .Element ) => Boolean ): Boolean =
172
+ def allSatisfy (predicate : (Self .Element ) => Boolean ): Boolean =
183
173
self.firstPositionWhere(predicate) == None
184
174
185
175
/** Returns the position of the first element of `self` satisfying `predicate`, or `None` if no
@@ -188,9 +178,9 @@ extension [Self: Collection as s](self: Self) {
188
178
* @complexity
189
179
* O(n) where n is the number of elements in `self`.
190
180
*/
191
- def firstPositionWhere (predicate : (s .Element ) => Boolean ): Option [s .Position ] =
181
+ def firstPositionWhere (predicate : (Self .Element ) => Boolean ): Option [Self .Position ] =
192
182
val e = self.endPosition
193
- def loop (p : s .Position ): Option [s .Position ] =
183
+ def loop (p : Self .Position ): Option [Self .Position ] =
194
184
if (p.eq(e)) {
195
185
None
196
186
} else if (predicate(self.at(p))) {
@@ -205,7 +195,7 @@ extension [Self: Collection as s](self: Self) {
205
195
* @complexity
206
196
* O(n) where n is the number of elements in `self`.
207
197
*/
208
- def minElement (isLessThan : (s .Element , s .Element ) => Boolean ): Option [s .Element ] =
198
+ def minElement (isLessThan : (Self .Element , Self .Element ) => Boolean ): Option [Self .Element ] =
209
199
self.leastElement(isLessThan)
210
200
211
201
// NOTE: I can't find a reasonable way to call this method.
@@ -214,36 +204,36 @@ extension [Self: Collection as s](self: Self) {
214
204
* @complexity
215
205
* O(n) where n is the number of elements in `self`.
216
206
*/
217
- def minElement ()(using s .Element is Comparable ): Option [s .Element ] =
207
+ def minElement ()(using Self .Element is Comparable ): Option [Self .Element ] =
218
208
self.minElement(isLessThan = _ `lt` _)
219
209
220
210
/** Returns the maximum element in `self`, using `isGreaterThan` to compare elements.
221
211
*
222
212
* @complexity
223
213
* O(n) where n is the number of elements in `self`.
224
214
*/
225
- def maxElement (isGreaterThan : (s .Element , s .Element ) => Boolean ): Option [s .Element ] =
215
+ def maxElement (isGreaterThan : (Self .Element , Self .Element ) => Boolean ): Option [Self .Element ] =
226
216
self.leastElement(isGreaterThan)
227
217
228
218
/** Returns the maximum element in `self`.
229
219
*
230
220
* @complexity
231
221
* O(n) where n is the number of elements in `self`.
232
222
*/
233
- def maxElement ()(using s .Element is Comparable ): Option [s .Element ] =
223
+ def maxElement ()(using Self .Element is Comparable ): Option [Self .Element ] =
234
224
self.maxElement(isGreaterThan = _ `gt` _)
235
225
236
226
/** Returns the maximum element in `self`, using `isOrderedBefore` to compare elements.
237
227
*
238
228
* @complexity
239
229
* O(n) where n is the number of elements in `self`.
240
230
*/
241
- def leastElement (isOrderedBefore : (s .Element , s .Element ) => Boolean ): Option [s .Element ] =
231
+ def leastElement (isOrderedBefore : (Self .Element , Self .Element ) => Boolean ): Option [Self .Element ] =
242
232
if (self.isEmpty) {
243
233
None
244
234
} else {
245
235
val e = self.endPosition
246
- def _least (p : s .Position , least : s .Element ): s .Element =
236
+ def _least (p : Self .Position , least : Self .Element ): Self .Element =
247
237
if (p.eq(e)) {
248
238
least
249
239
} else {
@@ -258,22 +248,18 @@ extension [Self: Collection as s](self: Self) {
258
248
259
249
}
260
250
261
- extension [Self : Collection as s ](self : Self )(
262
- using s.Element is Value
263
- ) {
251
+ extension [Self : Collection ](self : Self )
264
252
265
253
/** Returns `true` if `self` contains the same elements as `other`, in the same order. */
266
- def elementsEqual [T ]( using o : T is Collection { type Element = s .Element }) (other : T ): Boolean =
267
- def loop (i : s .Position , j : o .Position ): Boolean =
268
- if ( i `eq` self.endPosition) {
254
+ def elementsEqual [T : Collection { type Element = Self .Element } ] (other : T ): Boolean =
255
+ def loop (i : Self .Position , j : T .Position ): Boolean =
256
+ if i `eq` self.endPosition then
269
257
j `eq` other.endPosition
270
- } else if ( j `eq` other.endPosition) {
258
+ else if j `eq` other.endPosition then
271
259
false
272
- } else if ( self.at(i) `neq` other.at(j)) {
260
+ else if self.at(i) `neq` other.at(j)then
273
261
false
274
- } else {
262
+ else
275
263
loop(self.positionAfter(i), other.positionAfter(j))
276
- }
277
264
loop(self.startPosition, other.startPosition)
278
265
279
- }
0 commit comments