Skip to content

Commit 7175a21

Browse files
committed
Hylolib: Some stylistic tweaks
1 parent c997f35 commit 7175a21

File tree

2 files changed

+34
-57
lines changed

2 files changed

+34
-57
lines changed

tests/pos/hylolib/Collection.scala

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ trait Collection:
2424
*/
2525
def count: Int =
2626
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)
3030

3131
/** Returns the position of `self`'s first element', or `endPosition` if `self` is empty.
3232
*
@@ -80,7 +80,7 @@ trait Collection:
8080
recur(self.positionAfter(i))
8181
end Collection
8282

83-
extension [Self: Collection](self: Self) {
83+
extension [Self: Collection](self: Self)
8484

8585
/** Returns the first element of `self` along with a slice containing the suffix after this
8686
* element, or `None` if `self` is empty.
@@ -89,28 +89,24 @@ extension [Self: Collection](self: Self) {
8989
* O(1)
9090
*/
9191
def headAndTail: Option[(Self.Element, Slice[Self])] =
92-
if (self.isEmpty) {
92+
if self.isEmpty then
9393
None
94-
} else {
94+
else
9595
val p = self.startPosition
9696
val q = self.positionAfter(p)
9797
val t = Slice(self, Range(q, self.endPosition, (a, b) => (a `eq` b) || self.isBefore(a, b)))
9898
Some((self.at(p), t))
99-
}
10099

101100
/** Applies `combine` on `partialResult` and each element of `self`, in order.
102101
*
103102
* @complexity
104103
* O(n) where n is the number of elements in `self`.
105104
*/
106-
def reduce[T](partialResult: T, combine: (T, Self.Element) => T): T =
105+
def reduce[T](partialResult: T)(combine: (T, Self.Element) => T): T =
107106
val e = self.endPosition
108107
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)))
114110
loop(self.startPosition, partialResult)
115111

116112
/** Applies `action` on each element of `self`, in order, until `action` returns `false`, and
@@ -122,54 +118,46 @@ extension [Self: Collection](self: Self) {
122118
* @complexity
123119
* O(n) where n is the number of elements in `self`.
124120
*/
125-
def forEach(action: (Self.Element) => Boolean): Boolean =
121+
def forEach(action: Self.Element => Boolean): Boolean =
126122
val e = self.endPosition
127123
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))
135127
loop(self.startPosition)
136128

137129
/** Returns a collection with the elements of `self` transformed by `transform`, in order.
138130
*
139131
* @complexity
140132
* O(n) where n is the number of elements in `self`.
141133
*/
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)
147137

148138
/** Returns a collection with the elements of `self` satisfying `isInclude`, in order.
149139
*
150140
* @complexity
151141
* O(n) where n is the number of elements in `self`.
152142
*/
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
158146

159147
/** Returns `true` if `self` contains an element satisfying `predicate`.
160148
*
161149
* @complexity
162150
* O(n) where n is the number of elements in `self`.
163151
*/
164-
def containsWhere(predicate: (Self.Element) => Boolean): Boolean =
152+
def containsWhere(predicate: Self.Element => Boolean): Boolean =
165153
self.firstPositionWhere(predicate) != None
166154

167155
/** Returns `true` if all elements in `self` satisfy `predicate`.
168156
*
169157
* @complexity
170158
* O(n) where n is the number of elements in `self`.
171159
*/
172-
def allSatisfy(predicate: (Self.Element) => Boolean): Boolean =
160+
def allSatisfy(predicate: Self.Element => Boolean): Boolean =
173161
self.firstPositionWhere(predicate) == None
174162

175163
/** Returns the position of the first element of `self` satisfying `predicate`, or `None` if no
@@ -178,16 +166,12 @@ extension [Self: Collection](self: Self) {
178166
* @complexity
179167
* O(n) where n is the number of elements in `self`.
180168
*/
181-
def firstPositionWhere(predicate: (Self.Element) => Boolean): Option[Self.Position] =
169+
def firstPositionWhere(predicate: Self.Element => Boolean): Option[Self.Position] =
182170
val e = self.endPosition
183171
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))
191175
loop(self.startPosition)
192176

193177
/** Returns the minimum element in `self`, using `isLessThan` to compare elements.
@@ -229,26 +213,19 @@ extension [Self: Collection](self: Self) {
229213
* O(n) where n is the number of elements in `self`.
230214
*/
231215
def leastElement(isOrderedBefore: (Self.Element, Self.Element) => Boolean): Option[Self.Element] =
232-
if (self.isEmpty) {
216+
if self.isEmpty then
233217
None
234-
} else {
218+
else
235219
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
238222
least
239-
} else {
223+
else
240224
val x = self.at(p)
241225
val y = if isOrderedBefore(x, least) then x else least
242-
_least(self.positionAfter(p), y)
243-
}
244-
226+
loop(self.positionAfter(p), y)
245227
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)))
252229

253230
/** Returns `true` if `self` contains the same elements as `other`, in the same order. */
254231
def elementsEqual[T: Collection { type Element = Self.Element } ](other: T): Boolean =
@@ -262,4 +239,4 @@ extension [Self: Collection](self: Self)
262239
else
263240
loop(self.positionAfter(i), other.positionAfter(j))
264241
loop(self.startPosition, other.startPosition)
265-
242+
end extension

tests/pos/hylolib/HyArray.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ given [T: Value] => HyArray[T] is Value with {
172172
self.elementsEqual(other)
173173

174174
def hashInto(hasher: Hasher): Hasher =
175-
self.reduce(hasher, (h, e) => e.hashInto(h))
175+
self.reduce(hasher)((h, e) => e.hashInto(h))
176176

177177
}
178178

0 commit comments

Comments
 (0)