Skip to content

Instantly share code, notes, and snippets.

@fredrick
Created May 17, 2011 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrick/977562 to your computer and use it in GitHub Desktop.
Save fredrick/977562 to your computer and use it in GitHub Desktop.
Node.js Async I/O POC [equivalent of wc -l filename]
#!/usr/bin/env node
/**
* Node.js Async I/O POC [equivalent of wc -l filename]
* Run me: node wc.js [filename]
*/
var sys = require('sys'),
fs = require('fs');
// Counter monad
function Counter(value) {
this.value = value;
return this;
}
Counter.prototype = {
set: function(value) {
this.value = value;
return this;
},
increment: function(value) {
this.value += value;
return this;
}
}
function wc(file, callback) {
this.count = new Counter(0);
fs.readFile(file, function (error, data) {
this.count.increment(data.toString().split('\n').length - 1);
callback();
});
}
wc(process.argv[2], function(){
console.log(this.count);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment