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,12 +249,8 @@ pub fn sum[T](array []T) !T {
} else {
mut head := array[0]
for i, e in array {
if i == 0 {
continue
} else {
head += e
}
for e in array[1..] {
head += e
}
return head
@ -272,12 +268,8 @@ pub fn reduce[T](array []T, reduce_op fn (acc T, elem T) T) !T {
} else {
mut value := array[0]
for i, e in array {
if i == 0 {
continue
} else {
value = reduce_op(value, e)
}
for e in array[1..] {
value = reduce_op(value, e)
}
return value