Created
May 17, 2011 22:26
-
-
Save fredrick/977562 to your computer and use it in GitHub Desktop.
Node.js Async I/O POC [equivalent of wc -l filename]
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
#!/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