Function pairwiseReduce

Reduces in a pairwise fashion.

Eg, if we have input array of [1, 2, 3, 4, 5], the reducer fn will run with 1,2 as parameters, then 2,3, then 3,4 etc.

const values = [1, 2, 3, 4, 5]
reducePairwise(values, (acc, a, b) => {
return acc + (b - a);
}, 0);

If input array has less than two elements, the initial value is returned.

const reducer = (acc:string, a:string, b:string) => acc + `[${a}-${b}]`;
const result = reducePairwise(`a b c d e f g`.split(` `), reducer, `!`);
Yields: `![a-b][b-c][c-d][d-e][e-f][f-g]`
  • Type Parameters

    • V
    • X

    Parameters

    • array: readonly V[]
    • reducer: ((accumulator: X, a: V, b: V) => X)
        • (accumulator, a, b): X
        • Parameters

          • accumulator: X
          • a: V
          • b: V

          Returns X

    • initial: X

    Returns X