Created
January 29, 2012 10:25
-
-
Save sgoodwin/1698173 to your computer and use it in GitHub Desktop.
Count the time I've added up in my work textfile.
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
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