Skip to content

Instantly share code, notes, and snippets.

@omril1
Last active March 4, 2023 18:22
Show Gist options
  • Save omril1/a465fb4eee71f7beb8523608ae1efc0d to your computer and use it in GitHub Desktop.
Save omril1/a465fb4eee71f7beb8523608ae1efc0d to your computer and use it in GitHub Desktop.
even-iteration-abstraction.js
class EvensIterator {
constructor(realArray) {
this.realArray = realArray;
}
* [Symbol.iterator]() {
for (const n of this.realArray) {
if (n % 2 === 0) yield n;
}
}
}
const input = [1, 2, 3, 4, 5, 6, 78, 456, 5687, 345, 1423];
// abstraction thing
const evens = new EvensIterator(input);
for (const n of evens) {
console.log(n);
}
// VS actually doing it
for (const n of input) {
if (n % 2 === 0) {
console.log(n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment