Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created January 18, 2022 16:41
Show Gist options
  • Save gkucmierz/d7c1f1c486c6eb6ab1dd0a6f8953eb71 to your computer and use it in GitHub Desktop.
Save gkucmierz/d7c1f1c486c6eb6ab1dd0a6f8953eb71 to your computer and use it in GitHub Desktop.
// usefull object, to clip array in O(1) when we don't want to make copies of it (slice)
const ClipArray = arr => {
let low = 0;
let up = arr.length;
const length = () => up - low;
return {
clipHead: n => {
low += n;
if (low > up) low = up;
},
clipTail: n => {
up -= n;
if (up < low) up = low;
},
get: idx => {
if (idx >= length || idx < 0) return [][0];
return arr[low + idx];
},
getClipped: () => arr.slice(low, up),
length
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment