arrays: improve fold/reduce docs (#13700)

pull/13736/head
Nick Treleaven 2022-03-09 18:04:49 +00:00 committed by GitHub
parent 6c7243f2f7
commit 54de04a916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View File

@ -218,8 +218,10 @@ pub fn sum<T>(list []T) ?T {
} }
} }
// accumulates values with the first element and applying providing operation to current accumulator value and each elements. // reduce sets `acc = list[0]`, then successively calls `acc = reduce_op(acc, elem)` for each remaining element in `list`.
// If the array is empty, then returns error. // returns the accumulated value in `acc`.
// returns an error if the array is empty.
// See also: [fold](#fold).
// Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })? // => 120 // Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })? // => 120
pub fn reduce<T>(list []T, reduce_op fn (t1 T, t2 T) T) ?T { pub fn reduce<T>(list []T, reduce_op fn (t1 T, t2 T) T) ?T {
if list.len == 0 { if list.len == 0 {
@ -239,8 +241,16 @@ pub fn reduce<T>(list []T, reduce_op fn (t1 T, t2 T) T) ?T {
} }
} }
// accumulates values with providing initial value and applying providing operation to current accumulator value and each elements. // fold sets `acc = init`, then successively calls `acc = fold_op(acc, elem)` for each element in `list`.
// Example: arrays.fold<string, byte>(['H', 'e', 'l', 'l', 'o'], 0, fn (r int, t string) int { return r + t[0] }) // => 149 // returns `acc`.
// Example:
// ```v
// // Sum the length of each string in an array
// a := ['Hi', 'all']
// r := arrays.fold<string, int>(a, 0,
// fn (r int, t string) int { return r + t.len })
// assert r == 5
// ```
pub fn fold<T, R>(list []T, init R, fold_op fn (r R, t T) R) R { pub fn fold<T, R>(list []T, init R, fold_op fn (r R, t T) R) R {
mut value := init mut value := init
@ -275,7 +285,7 @@ pub fn flatten<T>(list [][]T) []T {
return result return result
} }
// grouping list of elements with given key selector. // group_by groups together elements, for which the `grouping_op` callback produced the same result.
// Example: arrays.group_by<int, string>(['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']} // Example: arrays.group_by<int, string>(['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
pub fn group_by<K, V>(list []V, grouping_op fn (v V) K) map[K][]V { pub fn group_by<K, V>(list []V, grouping_op fn (v V) K) map[K][]V {
mut result := map[K][]V{} mut result := map[K][]V{}

View File

@ -826,7 +826,8 @@ pub fn copy(dst []byte, src []byte) int {
// reduce executes a given reducer function on each element of the array, // reduce executes a given reducer function on each element of the array,
// resulting in a single output value. // resulting in a single output value.
// NOTE: It exists as a method on `[]int` types only // NOTE: It exists as a method on `[]int` types only.
// See also `arrays.fold`.
pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int { pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int {
mut accum_ := accum_start mut accum_ := accum_start
for i in a { for i in a {