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.
constvalues = [1, 2, 3, 4, 5] reducePairwise(values, (acc, a, b) => { returnacc + (b - a); }, 0);
If input array has less than two elements, the initial value is returned.
constreducer = (acc:string, a:string, b:string) =>acc + `[${a}-${b}]`; constresult = reducePairwise(`a b c d e f g`.split(` `), reducer, `!`); Yields: `![a-b][b-c][c-d][d-e][e-f][f-g]`
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.If input array has less than two elements, the initial value is returned.