Skip to content

Instantly share code, notes, and snippets.

@matyasfodor
Last active June 30, 2018 18:07
Show Gist options
  • Save matyasfodor/f8cc5f33940a0456634a60ffaaeb9d0d to your computer and use it in GitHub Desktop.
Save matyasfodor/f8cc5f33940a0456634a60ffaaeb9d0d to your computer and use it in GitHub Desktop.
const sizedArray = (n) => Array(n).fill();
const times = (n, cb) => {
while (0 < n--) {
cb();
}
}
const fillWithCb = (n, cb) => sizedArray(n).map(cb);
const nWise = n => array => {
iterators = fillWithCb(n, () => array[Symbol.iterator]());
iterators.forEach((it, index) => times(index, () => it.next()));
return fillWithCb(
array.length - n + 1,
() => iterators.map(it => it.next().value),
);
};
const pairWise = nWise(2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment