Stream a text file line by line with Nim streams
Nim
import std/[streams, strutils]
proc eachLine(path: string; onLine: proc(line: string) {.closure.}) =
var fs = newFileStream(path, fmRead)
if fs == nil:
raise newException(IOError, "cannot open " & path)
defer: fs.close()
var line = ""
while fs.readLine(line):
onLine(line.strip(leading = false, chars = {'\r'}))
var count = 0
eachLine("data.txt", proc(line: string) = inc count)
echo count