arrays: simplify arrays.sum and arrays.reduce (#22076)

This commit is contained in:
Carlos Esquerdo Bernat 2024-08-19 14:32:50 +02:00 committed by GitHub
parent 3965a6c54f
commit c8dc145468
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -249,13 +249,9 @@ pub fn sum[T](array []T) !T {
} else { } else {
mut head := array[0] mut head := array[0]
for i, e in array { for e in array[1..] {
if i == 0 {
continue
} else {
head += e head += e
} }
}
return head return head
} }
@ -272,13 +268,9 @@ pub fn reduce[T](array []T, reduce_op fn (acc T, elem T) T) !T {
} else { } else {
mut value := array[0] mut value := array[0]
for i, e in array { for e in array[1..] {
if i == 0 {
continue
} else {
value = reduce_op(value, e) value = reduce_op(value, e)
} }
}
return value return value
} }