Deutsch   English   Français   Italiano  
<v6ovk8$2he6l$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp
Subject: Re: Reading data into list from "include" file
Date: Thu, 11 Jul 2024 16:01:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <v6ovk8$2he6l$1@dont-email.me>
References: <87a5q7gzm1.fsf@axel-reichert.de> <XPVFq67p4V6ivNUR3@bongo-ra.co> <m2y1dqrxt7.fsf@MacBook-Pro-2.home> <v6mbui$1vpsc$1@dont-email.me> <v6od6u$2e0tq$1@dont-email.me>
Injection-Date: Thu, 11 Jul 2024 18:01:13 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="456461bd00b90f08114198f185f05762";
	logging-data="2668757"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+Bxj62fDxq6DygUog+W8rK"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:u+Jekx48rmgjBW4MkZoOeQEeJqI=
Bytes: 2374

B. Pym wrote:

> B. Pym wrote:
> 
> > Raymond Wiker wrote:
> > 
> > > For people who don't like extended loop, I think this a better
> > > implementation:
> > > 
> > > (defun read-numbers-from-file/2 (file-path n)
> > >   (with-open-file (f file-path :direction :input)
> > >     (let ((res nil))
> > >       (dotimes (i n)
> > >         (push (parse-integer (read-line f nil)) res))
> > >       (nreverse res))))
> > 
> > Gauche Scheme
> > 
> > (with-input-from-file "output.dat"
> >   (lambda res
> >     (until (read) eof-object? => x
> >       (push! res x))
> >     (reverse res)))
> > 
> >   ===>
> > (4 3 2 1 3 4 2 1 4 2 3 1 2 4 3 1 3 2 4 1 2 3 4 1 4 3 1 2
> >  3 4 1 2 4 1 3 2 1 4 3 2 3 1 4 2 1 3 4 2 4 2 1 3 2 4 1 3
> >  4 1 2 3 1 4 2 3 2 1 4 3 1 2 4 3 3 2 1 4 2 3 1 4 3 1 2 4
> >  1 3 2 4 2 1 3 4 1 2 3 4)
> >  
> 
> 
> (define (collect-till pred gen key)
>   (do ((x (gen) (gen))
>        (res '() (cons x res)))
>     ((pred x)  (reverse res))))
> 
> (with-input-from-file "output.dat"
>   (lambda _
>     (collect-till eof-object? read +)))

(with-input-from-file "output.dat"
  (lambda _  (collect-while + read)))


Given:

(define (collect-while pred gen . opt-key)
  (let ((key (if (pair? opt-key) (car opt-key) values)))
    (do ((x (gen) (gen))
         (res '() (cons (key x) res)))
      ((or (eof-object? x) (not (pred x)))  (reverse res)))))