Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Last active May 20, 2026 07:14
Show Gist options
  • Select an option

  • Save gkucmierz/d7c1f1c486c6eb6ab1dd0a6f8953eb71 to your computer and use it in GitHub Desktop.

Select an option

Save gkucmierz/d7c1f1c486c6eb6ab1dd0a6f8953eb71 to your computer and use it in GitHub Desktop.
Run this code instantly in your browser: https://instacode.app/gist/d7c1f1c486c6eb6ab1dd0a6f8953eb71
// 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
};
};
const arr = [1,2,3,4,5];
const ca = ClipArray(arr);
ca.clipHead(1);
ca.get(2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment