mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
array: add reduce() method
This commit is contained in:
committed by
Alexander Medvednikov
parent
67ae167013
commit
942c56ca95
@@ -354,3 +354,20 @@ pub fn (a []int) filter(predicate fn(p_val int, p_i int, p_arr []int) bool) []in
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
////////////// REDUCE //////////////
|
||||
|
||||
// method executes a reducer function (that you provide) on each element of the array,
|
||||
// resulting in a single output value.
|
||||
pub fn (a []int) reduce(
|
||||
iter fn (accum int, curr int) int,
|
||||
accum_start int
|
||||
) int
|
||||
{
|
||||
mut _accum := 0
|
||||
_accum = accum_start
|
||||
for i := 0; i < a.len; i++ {
|
||||
_accum = iter(_accum, a[i])
|
||||
}
|
||||
return _accum
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user