I am iterating though the lines in a file using Node.js with CoffeScript and the following function:
each_line_in = (stream, func) ->
fs.stat stream.path, (err, stats) ->
previous = []
stream.on 'data', (d) ->
start = cur = 0
for c in d
cur++
if c == 10
previous.push(d.slice(start, cur))
func previous.join('')
previous = []
start = cur
previous.push(d.slice(start, cur)) if start != cur
Is there a better way to do this without reading the entire file into memory? And by "better" I mean more succinct, built into Node.js, faster, or more correct. If I was writing Python I would do something like this:
def each_line_in(file_obj, func):
[ func(l) for l in file_obj ]
I saw this question which uses Peteris Krumin's "lazy" module, but I would like to accomplish this w/o adding an external dependency.