mirror of
https://github.com/vlang/v.git
synced 2025-09-10 07:47:20 -04:00
arrays: simplify arrays.sum and arrays.reduce (#22076)
This commit is contained in:
parent
3965a6c54f
commit
c8dc145468
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user