Skip to content

Instantly share code, notes, and snippets.

@sgoodwin
Created January 29, 2012 10:25
Show Gist options
  • Save sgoodwin/1698173 to your computer and use it in GitHub Desktop.
Save sgoodwin/1698173 to your computer and use it in GitHub Desktop.
Count the time I've added up in my work textfile.
var fs = require('fs'),
async = require('async');
fs.readFile('work.txt', 'utf8', function(err, data){
if(err){
throw err;
}
var lines = data.split('\n');
var iterator = function(item, callback){
var matcher = item.match(/ (\d{1}(\.\d{1,3})?)( \(.*\))?$/)[0];
callback(null, parseFloat(matcher));
};
async.map(lines, iterator, function(err, results){
var reduction = function(memo, number, callback){
callback(null, memo+number);
};
async.reduce(results, 0.0, reduction, function(err, sum){
console.log(sum);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment