Skip to content

Commit c8dc145

Browse files
authored
arrays: simplify arrays.sum and arrays.reduce (#22076)
1 parent 3965a6c commit c8dc145

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

vlib/arrays/arrays.v

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,8 @@ pub fn sum[T](array []T) !T {
249249
} else {
250250
mut head := array[0]
251251

252-
for i, e in array {
253-
if i == 0 {
254-
continue
255-
} else {
256-
head += e
257-
}
252+
for e in array[1..] {
253+
head += e
258254
}
259255

260256
return head
@@ -272,12 +268,8 @@ pub fn reduce[T](array []T, reduce_op fn (acc T, elem T) T) !T {
272268
} else {
273269
mut value := array[0]
274270

275-
for i, e in array {
276-
if i == 0 {
277-
continue
278-
} else {
279-
value = reduce_op(value, e)
280-
}
271+
for e in array[1..] {
272+
value = reduce_op(value, e)
281273
}
282274

283275
return value

0 commit comments

Comments
 (0)