Re: Newbie asking for help

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Newbie asking for help
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 26. Jun 2025, 22:14:51
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103kd89$3m43r$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Erik Naggum wrote:
 
 This isn't meant to reopen the great LOOP flame war but one way of
 doing it is:
 
 (with-open-file (ifile "key.html" :direction :input)
   (loop for line = (read-line ifile nil)
         while line
         counting 1))
 
  Simply using while (read-line ifile nil nil) seems even more compact.
 
  However, I have this negative gut reaction to wanton waste, as in
  effectively allocating as many strings as there are lines for no
  good reason, just exercising the garbage collector, so while we're
  at it, how about
 
(loop for char = (read-char ifile nil nil)
      while char
      count (char= char #\newline))
 
  Note that read-line will return nil on an empty file, but the data
  leading up to the end-of-file if no newline intervened and then nil
  on the next call, effectively counting a non-empty file containing
  no newlines as having one line.  This may be relevant.
 
 (loop for line being each line of ifile
       count 1)
 
  Elegant.
 
Gauche Scheme
 
(generator-count identity read-line)
 
Testing:
 
(use gauche.generator)
 
(with-input-from-string
  "one
  two
  three"
  (lambda _   (generator-count identity read-line)))
 
  ===>
3

Shorter (using "+" for "identity").

(with-input-from-string
  "one
  two
  three"
  (cut   generator-count + read-line))
 


Date Sujet#  Auteur
1 Jul 24 * Re: Newbie asking for help2B. Pym
26 Jun 25 `- Re: Newbie asking for help1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal