[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C++ STL
From: |
Eric E Moore |
Subject: |
Re: C++ STL |
Date: |
Mon, 24 Jun 2002 22:22:01 +0100 |
User-agent: |
Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.2 |
"MaurÃcio" <address@hidden> writes:
> Yes, I think you are right. However, I think there are some ideas that
> could be interesting to use. For instance, it would be nice if we could do
> things like this:
>
> (for_each_c++ (i (iota 1 10)) (line (file_lines "file_name.txt)) (in
> stdinput)
> (some_function i line in))
>
> meaning that some_function would be executed with i going from 1 to 9, line
> would be consecutive lines from a file and in would be lines got from user
> input (and the "loop" would finish when i or line became #f). In this
> example, for_each_c++ itself could be a generator that would return the
> consecutive values of some_function, and so it could be used inside another
> for_each_c++ (or other iterator-aware algorithm).
Oh. Well, we have streams, and stream-for-each.
The following code is untested (so probably doesn't work :), but
should be close enough to what would that it makes the point we can do
this.
(use-modules (ice-9 streams))
(define (counting-stream n)
(lambda (x)
(if (>= x n) #f (cons (1+ x) (1+ x))))
(stream-for-each some-function
(make-stream (countto 10) 1)
(port->stream (open-input-file "file_name.txt") read-line)
(port->stream (current-input-port) read-line))
Also, read-line seems to not be in CVS guile (well, the oldish one I
have installed), but it's not too long to write (if one is unfussy
about it):
;
; Why do we no longer have read-line?
;
(define (read-line port)
(let f ((chars ()))
(let ((c (read-char port)))
(cond
((equal? c #\newline)
(list->string (reverse chars)))
((eof-object? c) c)
(else
(f (cons c chars)))))))
--
Eric E. Moore
pgp7mg1GZBAWW.pgp
Description: PGP signature