- Write pure functions for as much of your program as possible
- No I/O, no side effects, no mutable data structures, etc
- Keep your I/O cleanly separate and organized
So in general purely functional programmers like to keep their I/O code in a well-contained and small box so they can focus as much as possible on writing pure functions that accept in-memory data types as arguments and return values (or invoke callbacks with values). So with that in mind, the basic idea is:
//Here's a pure function. Does no I/O. No side effects.
//No mutable data structures. Easy to test and mock.
function processSomeData(theData) {
//useful code here
return theData + " is now useful";
}
//Here's the "yucky" I/O kept in a small box with a heavy lid
function gatherInput(callback) {
var input = [];
process.stdin.on('data', function (chunk) {input.push(chunk);});
process.stdin.on('end', function () {callback(input.join('');});
}
//Here's the glue to make it all run together
gatherInput(processSomeData);