Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.lisp
Re: lisp idiom for processing each line in a file?
William James
10/17/2015 2:55:00 AM
Kirk Job Sluder wrote:
> The macro I use is stolen from here:
>
http://kantz.com/jason/clim-primer...
>
> I've added substantial comments.
>
> (defmacro do-file ((path line-variable &key (key #'identity)) &body body)
> "Iterate over the lines of the file, binding the line to the
> line-variable in each iterateion"
> (let ((str (gensym)) ;a temporary stream variable
> (var (gensym))) ;a temporary variable to hold the file input.
> `(with-open-file (,str ,path :direction :input)
> (do ((,var (read-line ,str nil) ;get the first line
> (read-line ,str nil)) ;get the next line
> ((not ,var)) ;terminate when there is no text.
> (let ((,line-variable (funcall ,key ,var)))
> ;set line-variable to var, calling our key function
> ;if necessary.
> ,@body))))) ;do the body form
>
> Then, I usually call it with something like:
>
> (do-file ("authors.txt" line)
> (foo-function line))
Gauche Scheme:
(define (do-file filename proc)
(with-input-from-file filename
(cut generator-for-each
proc
read-line)))
(do-file "data.txt"
(lambda (line) (print ">>> " line)))
--
The negroes have first rate tents with stoves in them, get soft bread to eat
most of the time, and don't have to do night work. The white men have no
stoves, have to eat hard tack, and do night work.
--- The Old Guard, February 1863, p. 36
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: lisp idiom for processing each line in a file?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password