Created
June 21, 2020 13:05
-
-
Save torus/b9fc1fa66be0a93e1e9f6b0d05ab09d4 to your computer and use it in GitHub Desktop.
JavaScript でジェネレータを使ってファイルを 1 行ずつ読み込んで処理する練習。
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
const readline = require('readline'); | |
async function processLineByLine(strm, eat) { | |
const rl = readline.createInterface({ | |
input: strm, | |
crlfDelay: Infinity | |
}); | |
for await (const line of rl) { | |
// Each line in input.txt will be successively available here as `line`. | |
eat(false, line) | |
} | |
eat(true) | |
} | |
function eat(gen) { | |
gen.next() | |
return (done, line) => gen.next({done, line}) | |
} | |
function* main() { | |
while (true) { | |
var {done, line} = yield | |
if (done) | |
break | |
else | |
console.log(">>>", line) | |
} | |
console.log("DONE!") | |
} | |
processLineByLine(process.stdin, eat(main())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment