Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #363: Out of cards on drive D:


comp / comp.lang.lisp / Re: Newbie asking for help

SubjectAuthor
o Re: Newbie asking for helpB. Pym

1
Subject: Re: Newbie asking for help
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 1 Jul 2024 18:07 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Newbie asking for help
Date: Mon, 1 Jul 2024 18:07:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <v5ur9r$16goe$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 01 Jul 2024 20:07:59 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="8f0d5f3f26b0c1562f9038e9954a9a6e";
logging-data="1262350"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qE+jC9jRoQG3BXV07iO7Q"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:g7U2WItVDaDS7Cn8b4mv0rByZB4=
View all headers

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

1

rocksolid light 0.9.8
clearnet tor