Skip to content

Instantly share code, notes, and snippets.

@giuseppe998e
Last active March 23, 2021 21:33
Show Gist options
  • Save giuseppe998e/3a41bb706d2e9512923258852a5f99e7 to your computer and use it in GitHub Desktop.
Save giuseppe998e/3a41bb706d2e9512923258852a5f99e7 to your computer and use it in GitHub Desktop.
5 Lines Iterator JS
// Five Lines Iterator [ fliter (╥﹏╥) ]
const fliter = (a, p = 0) => ({
hasNext: () => p < a.length,
next: () => a[p++],
remove: () => a.splice(--p, 1)
})
/* Minified
* const fliter=(a,p=0)=>({hasNext:()=>p<a.length,next:()=>a[p++],remove:()=>a.splice(--p,1)})
*/
/* --- EXAMPLE --- */
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const iterArr = fliter(array);
while (iterArr.hasNext()) {
const val = iterArr.next()
console.log(val) // 0 .. 1 .. 2 .. 3 .. 4 .. 5 .. 6 .. 7 .. 8 .. 9
if (val > 5)
iterArr.remove()
}
console.log() // < empty line >
console.log(array) // [ 0, 1, 2, 3, 4, 5 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment