Skip to content

adjacentCombine$

Subhajit Sahu edited this page May 3, 2023 · 1 revision

Combine adjacent values of an array!


function adjacentCombine$(x, fc, acc)
// x:   an array (updated!)
// fc:  combine function (u, v)
// acc: initial value

const xarray = require('extra-array');

var x = [1, 2, 3, 4];
xarray.adjacentCombine$(x, (u, v) => v-u, 0);
// → [ 1, 1, 1, 1 ]  // (x is updated!)

var x = [1, 2, 3, 4];
xarray.adjacentCombine$(x, (u, v) => u*v, 1);
// → [ 1, 2, 6, 12 ]  // (x is updated!)

var x = [1, 2, 3, 4];
xarray.adjacentCombine$(x, (u, v) => u+v, 10);
// → [ 11, 3, 5, 7 ]  // (x is updated!)


References

Clone this wiki locally