UNPKG

@stdlib/utils

Version:

Standard utilities.

66 lines (48 loc) 1.81 kB
{{alias}}( arr, initial, mapper, reducer[, thisArg] ) Performs a map-reduce operation for each element in an array and returns the accumulated result. When invoked, the mapping function is provided three arguments: - value: array element. - index: element index. - arr: input array. When invoked, the reducing function is provided four arguments: - accumulator: accumulated value. - value: result after applying the mapping function to the current array element. - index: element index. - arr: input array. If provided an empty array, the function returns the initial value. When provided an ndarray, the function performs a single-pass map-reduce operation over the entire input ndarray (i.e., higher-order ndarray dimensions are flattened to a single-dimension). Parameters ---------- arr: ArrayLikeObject|ndarray Input array. initial: any Accumulator value used in the first invocation of the reduction function. mapper: Function Mapping function. reducer: Function Reducing function. thisArg: any (optional) Execution context for the reducing function. Returns ------- out: any Accumulated result. Examples -------- // array-like object: > var f1 = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/special/abs}}, 1 ); > var f2 = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/ops/add}}, 2 ); > var arr = [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ]; > var out = {{alias}}( arr, 0.0, f1, f2 ) 21.0 // ndarray: > arr = {{alias:@stdlib/ndarray/array}}( arr, { 'shape': [ 2, 3 ] } ); > out = {{alias}}( arr, 0.0, f1, f2 ) 21.0 See Also --------