Skip to content

Instantly share code, notes, and snippets.

@simontraill
Last active October 12, 2017 15:33
Show Gist options
  • Save simontraill/80ea6b7049cbea6563f87ffa9abf82e5 to your computer and use it in GitHub Desktop.
Save simontraill/80ea6b7049cbea6563f87ffa9abf82e5 to your computer and use it in GitHub Desktop.
var readline = require('readline');
function myAssigner(item) {
return (item >= 100)? 10: item % 10;
}
function BucketReport(count,assigner,scorer) {
return {
buckets: new Array(count).fill(0),
add: function(i) {
var b = assigner(i);
this.buckets[b] = scorer(this.buckets[b],i);
}
}
}
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var h = BucketReport(11,myAssigner,function(x,y) { return x + y });
rl.on('line', function(line){
h.add(parseInt(line.trim()));
})
rl.on('close',function() {
console.log(JSON.stringify(h.buckets));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment