Lua streaming line reader for text files
Lua
local function each_line(path)
local file, err = io.open(path, 'r')
if not file then error(('cannot open %s: %s'):format(path, err), 0) end
local n = 0
return function()
if not file then return nil end
local line = file:read('l')
if line then
n = n + 1
return line, n
end
file:close()
file = nil
end
end
return each_line