Read a file one line at a time in node.js?

Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here’s the easiest way to read lines from a file, without any external modules: const fs = require(‘fs’); const readline = require(‘readline’); async function processLineByLine() { const fileStream = fs.createReadStream(‘input.txt’); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity }); // … Read more

How do lexical closures work?

Python is actually behaving as defined. Three separate functions are created, but they each have the closure of the environment they’re defined in – in this case, the global environment (or the outer function’s environment if the loop is placed inside another function). This is exactly the problem, though – in this environment, i is … Read more

Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

If you have Guava in your project, you can use the Streams.zip method (was added in Guava 21): Returns a stream in which each element is the result of passing the corresponding element of each of streamA and streamB to function. The resulting stream will only be as long as the shorter of the two … Read more