Created
February 17, 2012 16:17
-
-
Save mattnworb/1854185 to your computer and use it in GitHub Desktop.
Loops over a file containing one JSON entry per line and prints out the created_at value . This gist shows the original coffeescript and the generated JavaScript
This file contains hidden or 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
fs = require 'fs' | |
unless process.argv.length > 2 | |
console.error "Usage: print_times <file>" | |
process.exit -1 | |
fileName = process.argv[2] | |
String::endsWith = (ch) -> | |
return @length - 1 == @lastIndexOf(ch) | |
list_to_json = (list) -> | |
(JSON.parse l for l in list) | |
fs.readFile fileName, 'utf-8', (err, data) -> | |
throw err if err | |
lines = data.split '\n' | |
lines = lines[0...lines.length - 1] | |
chomped = (line.trim() for line in lines) | |
fulllines = (line for line in chomped when line.endsWith "}") | |
tweets = list_to_json(fulllines) | |
console.log "Read #{ tweets.length } tweets" | |
times = (tweet.created_at for tweet in tweets when tweet.created_at?) | |
console.log time for time in times | |
null |
This file contains hidden or 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
(function() { | |
var fileName, fs, list_to_json, parse_tweet; | |
fs = require('fs'); | |
if (!(process.argv.length > 2)) { | |
console.error("Usage: print_times <file>"); | |
process.exit(-1); | |
} | |
fileName = process.argv[2]; | |
String.prototype.endsWith = function(ch) { | |
return this.length - 1 === this.lastIndexOf(ch); | |
}; | |
list_to_json = function(list) { | |
var l; | |
return [ | |
(function() { | |
var _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = list.length; _i < _len; _i++) { | |
l = list[_i]; | |
_results.push(JSON.parse(l)); | |
} | |
return _results; | |
})() | |
]; | |
}; | |
parse_tweet = function(line, ix) { | |
return JSON.parse(line); | |
}; | |
fs.readFile(fileName, 'utf-8', function(err, data) { | |
var chomped, fulllines, line, lines, time, times, tweet, tweets, _i, _len; | |
if (err) throw err; | |
lines = data.split('\n'); | |
lines = lines.slice(0, (lines.length - 1)); | |
chomped = (function() { | |
var _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = lines.length; _i < _len; _i++) { | |
line = lines[_i]; | |
_results.push(line.trim()); | |
} | |
return _results; | |
})(); | |
fulllines = (function() { | |
var _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = chomped.length; _i < _len; _i++) { | |
line = chomped[_i]; | |
if (line.endsWith("}")) _results.push(line); | |
} | |
return _results; | |
})(); | |
console.log("" + fulllines.length + " fl"); | |
tweets = list_to_json(fulllines); | |
console.log("Read " + tweets.length + " tweets"); | |
times = (function() { | |
var _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = tweets.length; _i < _len; _i++) { | |
tweet = tweets[_i]; | |
if (tweet.created_at != null) _results.push(tweet.created_at); | |
} | |
return _results; | |
})(); | |
for (_i = 0, _len = times.length; _i < _len; _i++) { | |
time = times[_i]; | |
console.log(time); | |
} | |
return null; | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment