Created
May 15, 2020 22:46
-
-
Save sangkukbae12/3fac1aff16d53c4a346f3f85ea048cb3 to your computer and use it in GitHub Desktop.
Generators in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Use the Default Iterator from an Array | |
const abcs = ["A", "B", "C"] | |
const createIterator = abcs[Symbol.iterator].bind(abcs) | |
const iterator = createIterator() | |
for (const i of iterator) { | |
console.log(i) | |
} | |
// 2. Create Your First Iterator in JavaScript | |
let i = 0 | |
const next = () => ({ | |
value: i++, | |
done: i > 10 | |
}) | |
const iterator = { | |
[Symbol.iterator]() { | |
return { | |
next | |
} | |
} | |
} | |
for (let value of iterator) { | |
console.log(value) | |
} | |
// 3. Create a Custom Iterator for Any Array | |
const abcs = ["A", "B", "C"] | |
const numbers = [1, 2, 3] | |
const createReverseIterator = array => ({ | |
[Symbol.iterator]() { | |
let i = array.length | |
return { | |
next: () => ({ | |
value: array[--i], | |
done: i < 0 | |
}) | |
} | |
} | |
}) | |
for (let value of createReverseIterator(numbers)) { | |
console.log(value) | |
} | |
// 4. Create Your First Generator in JavaScript | |
const abcs = ["A", "B", "C"] | |
const numbers = [1, 2, 3] | |
const createReverseIterator = array => ({ | |
[Symbol.iterator]() { | |
let i = array.length | |
return { | |
next: () => ({ | |
value: array[--i], | |
done: i < 0 | |
}) | |
} | |
} | |
}) | |
const reverseIterator = function* (array) { | |
let i = array.length | |
while (i > 0) { | |
yield array[--i] | |
} | |
} | |
for (let value of reverseIterator(numbers)) { | |
console.log(value) | |
} | |
// 5. Stop a Generator from Yielding Values | |
const abcs = ["A", "B", "C"] | |
const reverseIterator = function* (array) { | |
let i = array.length | |
while (i > 0) { | |
yield array[--i] | |
} | |
} | |
const iterator = reverseIterator(abcs) | |
console.log(iterator.next()) | |
console.log(iterator.return()) | |
console.log(iterator.next()) | |
console.log(iterator.next()) | |
// 6. Yield an Array, String, or Any Iterable from a Generator | |
const abcs = ["A", "B", "C"] | |
const reverseIterator = function* (array) { | |
yield* array | |
yield* array.map(letter => letter.toLowerCase()) | |
yield Math.random() | |
} | |
const iterator = reverseIterator(abcs) | |
for (let value of iterator) { | |
console.log(value) | |
} | |
// 7. Avoid Nested For Loops with Generators | |
const abcs = ["A", "B", "C"] | |
const shoutIterator = function* (word) { | |
yield word + "!" | |
yield word + "!!" | |
yield word + "!!!" | |
} | |
const reverseIterator = function* (array) { | |
yield* shoutIterator(array[0]) | |
yield* shoutIterator(array[1]) | |
yield* shoutIterator(array[2]) | |
} | |
const iterator = reverseIterator(abcs) | |
for (let value of iterator) { | |
console.log(value) | |
} | |
// 8. Understand Each Iteration Inside a Generator | |
function* generator() { | |
//first iteration | |
yield 1 | |
//second iteration | |
console.log("second") | |
yield 2 | |
//third iteration | |
console.log("third") | |
} | |
const iterator = generator() | |
console.log(iterator.next()) | |
console.log(iterator.next()) | |
console.log(iterator.next()) | |
// 9. Use a Generator Like a Closure | |
function makeAdder(x) { | |
return function (y) { | |
return x + y | |
} | |
} | |
let add2 = makeAdder(2) | |
function* makeAdderGenerator(x) { | |
let y = yield x | |
while (true) { | |
y = yield x + y | |
} | |
} | |
let add2Iterator = makeAdderGenerator(2) | |
console.log(add2Iterator.next()) | |
console.log(add2Iterator.next(1)) | |
console.log(add2Iterator.next(2)) | |
// 10. Create a State Machine with a Generator | |
function* stateMachine(state) { | |
let transition | |
while (true) { | |
if (transition === "INCREMENT") { | |
state++ | |
} | |
if (transition === "DECREMENT") { | |
state-- | |
} | |
transition = yield state | |
} | |
} | |
const iterator = stateMachine(0) | |
console.log(iterator.next()) | |
console.log(iterator.next("INCREMENT")) | |
console.log(iterator.next("INCREMENT")) | |
console.log(iterator.next("DECREMENT")) | |
// 11. Create a `range` Helper Function with a Generator | |
function* range(start, end) { | |
while (start < end) yield start++ | |
} | |
for (let value of range(0, 10)) { | |
console.log(value) | |
} | |
console.log([...range(0, 100)]) | |
// 12. Compare a Generator to Using Array Map and Filter | |
let names = ["John", "Mindy", "Sally"] | |
let result = names.filter(name => name.includes("y")).map(name => name.toLocaleLowerCase()) | |
console.log(result) | |
function* format(array) { | |
for (let value of array) { | |
if (value.includes("y")) { | |
yield value.toLowerCase() | |
return | |
} | |
} | |
} | |
console.log([...format(names)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment