Last active
February 21, 2019 04:34
-
-
Save aleph-naught2tog/1b938248fb296993a9ee2a53b9bc70a9 to your computer and use it in GitHub Desktop.
yield*
This file contains hidden or 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
const fs = require('fs'); // the node file system package | |
const path = require('path'); | |
// Usage: | |
// node file_tree.js name_of_folder_to_get_files_from | |
// node file_tree.js | |
// Either a passed-in-folder name (process.argv[2]) | |
// or default to the current working directory | |
const folder = process.argv[2] || process.cwd(); | |
// (Function definition is below) | |
const allNestedFiles = walkFileTree(folder); | |
// We can use `for ... of` here because of the generator :D | |
for (const file of allNestedFiles) { | |
console.log(file); | |
} | |
// ----------------------------------------------------------------------------- | |
function* walkFileTree(startingFolder = '.') { | |
const readOptions = { withFileTypes: true }; | |
const folderContents = fs.readdirSync(startingFolder, readOptions); | |
for (const currentItem of folderContents) { | |
const fullyQualifiedName = path.join(startingFolder, currentItem.name); | |
if (currentItem.isDirectory()) { | |
// *this* is the super nifty party -- it will `yield`, in turn, EVERYTHING | |
// that `walkFileTree` itself then yields -- this is how we go deeper | |
yield* walkFileTree(fullyQualifiedName); | |
} else { | |
yield fullyQualifiedName; | |
} | |
} | |
} |
This file contains hidden or 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
function* generateLetters() { | |
const letters = 'abc'; | |
for (const letter of letters) { | |
yield letter; | |
} | |
} | |
function* generateNumbers() { | |
// a very clunky range | |
// the numbers from 0 to 2 | |
const numbers = new Array(3).fill(null).map((_, index) => index); | |
for (const number of numbers) { | |
yield number; | |
} | |
} | |
function* interruptingLetters() { | |
const numbers = new Array(3).fill(null).map((_, index) => index); | |
for (const number of numbers) { | |
yield number; | |
if (number % 2 === 1) { | |
// if our number is odd | |
yield* generateLetters(); | |
} | |
} | |
} | |
const lettersJustGenerated = [...generateLetters()]; | |
console.log(lettersJustGenerated); // [ 'a', 'b', 'c' ] | |
const numbersJustGenerated = [...generateNumbers()]; | |
console.log(numbersJustGenerated); // [ 0, 1, 2 ] | |
const intermixed = [...interruptingLetters()]; | |
console.log(intermixed); // [ 0, 1, 'a', 'b', 'c', 2 ] | |
// yield 0 | |
// yield 1 | |
// yield* ... | |
// yield 'a', yield 'b', yield 'c' | |
// yield 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment