mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
arrays: fix arrays.fold, when the init
value in the call, is an array (#21921)
This commit is contained in:
parent
b5ba466488
commit
d186c3946f
2 changed files with 27 additions and 1 deletions
|
@ -331,7 +331,12 @@ pub fn filter_indexed[T](array []T, predicate fn (idx int, elem T) bool) []T {
|
|||
// assert r == 5
|
||||
// ```
|
||||
pub fn fold[T, R](array []T, init R, fold_op fn (acc R, elem T) R) R {
|
||||
mut value := init
|
||||
mut value := R{}
|
||||
$if R is $array {
|
||||
value = init.clone()
|
||||
} $else {
|
||||
value = init
|
||||
}
|
||||
|
||||
for e in array {
|
||||
value = fold_op(value, e)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue