Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 40 |
Nodes: | 6 (0 / 6) |
Uptime: | 10:50:11 |
Calls: | 291 |
Files: | 910 |
Messages: | 76,430 |
(defun foo ()
(with-open-file (strm "/tmp/test.big")
(loop as line = (read-line strm nil nil)
while line
summing (length line))))
Gauche Scheme
(use gauche.generator)
(define (foo f)
(generator-fold
+ 0
(gmap string-length (file->generator f read-line))))
(foo "output.dat")
119
(lflow "/etc/hosts"file-get-lines
Edi Weitz wrote:
(defun foo ()
(with-open-file (strm "/tmp/test.big")
(loop as line = (read-line strm nil nil)
while line
summing (length line))))
Bad. Very bad. Fails to remove any carriage return
at the end of the line. Consequently, the sum
may be incorrect.
Instead of CL, let's use a Lispy language.
Sum the lengths of all of the lines in a text file.
The length of a line is measured after removing the
end-of-line characters at the end.
Gauche Scheme
(use gauche.generator)
(define (foo f)
(generator-fold
+ 0
(gmap string-length (file->generator f read-line))))
(foo "output.dat")
===>
119