From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kevin Ryde Newsgroups: gmane.lisp.guile.devel Subject: stream-for-each 2 or more bug Date: Wed, 08 Sep 2004 11:37:55 +1000 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: <87hdq9fslo.fsf@zip.com.au> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: sea.gmane.org 1094607538 26222 80.91.224.253 (8 Sep 2004 01:38:58 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 8 Sep 2004 01:38:58 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Sep 08 03:38:50 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1C4rQ9-00038L-00 for ; Wed, 08 Sep 2004 03:38:49 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C4rVO-0005c0-U5 for guile-devel@m.gmane.org; Tue, 07 Sep 2004 21:44:14 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1C4rVH-0005bR-23 for guile-devel@gnu.org; Tue, 07 Sep 2004 21:44:07 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1C4rVG-0005bF-FE for guile-devel@gnu.org; Tue, 07 Sep 2004 21:44:06 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C4rVG-0005bC-Bo for guile-devel@gnu.org; Tue, 07 Sep 2004 21:44:06 -0400 Original-Received: from [61.8.0.85] (helo=mailout2.pacific.net.au) by monty-python.gnu.org with esmtp (Exim 4.34) id 1C4rPf-0001Pp-52 for guile-devel@gnu.org; Tue, 07 Sep 2004 21:38:20 -0400 Original-Received: from mailproxy2.pacific.net.au (mailproxy2.pacific.net.au [61.8.0.87]) by mailout2.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i881cHje024968 for ; Wed, 8 Sep 2004 11:38:17 +1000 Original-Received: from localhost (ppp2542.dyn.pacific.net.au [61.8.37.66]) by mailproxy2.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i881cF78008030 for ; Wed, 8 Sep 2004 11:38:15 +1000 Original-Received: from gg by localhost with local (Exim 3.36 #1 (Debian)) id 1C4rPK-0001nf-00; Wed, 08 Sep 2004 11:37:58 +1000 Original-To: guile-devel@gnu.org Mail-Copies-To: never User-Agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (gnu/linux) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:4096 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:4096 --=-=-= * streams.scm (stream-for-each-many): Correction, should recurse into itself, not stream-for-each-one. I started some words about streams too. stream-filter and stream-ref from sicp would be nice additions. 3.1.6 Streams ------------- Streams represent a sequence of values, each of which is calculated only when required. This allows large or even infinite sequences to be represented, and manipulated with familiar operations like "car", "cdr", "map" or "fold". In such manipulations only as much as needed is actually held in memory at any one time. The functions in this section are available from (use-modules (ice-9 streams)) Streams are implemented using promises (*note Delayed Evaluation::), this is how the underlying calculation of values is made only when needed, and the values thereafter retained so the calculation is not repeated. Here is a simple example producing a stream of all odd numbers, (define odds (make-stream (lambda (state) (cons state (+ state 2))) 1)) (stream-car odds) => 1 (stream-car (stream-cdr odds)) => 3 `stream-map' could be used to derive a stream of odd squares, (define (square n) (* n n)) (define oddsquares (stream-map square odds)) These are infinite sequences, so it's not possible to convert them to a list, but they could be printed (infinitely) with for example (stream-for-each (lambda (n nsq) (format #t "~a squared is ~a\n" n nsq)) odds oddsquares) -| 1 squared is 1 3 squared is 9 5 squared is 25 7 squared is 49 ... -- Function: make-stream proc initial-state Return a new stream which is the results of calling PROC successively. Each call is `(PROC state)', it should return a pair, the `car' being the value for the stream, and the `cdr' being the new STATE for the next call. For the first call the STATE is the given INITIAL-STATE. At the end of the stream, PROC should return something other than a pair. -- Function: stream-car stream Return the first element from STREAM. STREAM must not be empty. -- Function: stream-cdr stream Return a stream which is the second and subsequent elements of STREAM. STREAM must not be empty. -- Function: stream-null? stream Return true if STREAM is empty. -- Function: list->stream list -- Function: vector->stream vector Return a stream with the contents of LIST or VECTOR. LIST or VECTOR should not be modified subsequently, it's unspecified whether changes there will be reflected in the stream returned. -- Function: port->stream port readproc Return a stream which gives the values obtained by reading from PORT using READPROC. Each read call made is `(READPROC PORT)', it should return an EOF object (*note Reading::) at the end of input. For example a stream of characters from a file, (port->stream (open-input-file "/foo/bar.txt") read-char) -- Function: stream->list stream Return a list which is the entire contents of STREAM. -- Function: stream->reversed-list stream Return a list which is the entire contents of STREAM, but in reverse order. -- Function: stream->list&length stream Return two values (*note Multiple Values::) being a list which is the entire contents of STREAM, and the number of elements in that list. -- Function: stream->reversed-list&length stream Return two values (*note Multiple Values::) being a list which is the entire contents of STREAM, but in reverse order, and the number of elements in that list. -- Function: stream->vector stream Return a vector which is the entire contents of STREAM. -- Function: stream-fold proc init stream0 ... streamN Apply PROC successively over the elements of the given streams, from first to last. Return the result from the last PROC call. Each call is `(PROC elem0 ... elemN prev)', where each ELEM is from the corresponding STREAM. PREV is the return from the previous PROC call, or the given INIT for the first call. -- Function: stream-for-each proc stream0 ... streamN Call PROC on the elements from the given STREAMs. The return value is unspecified. Each call is `(PROC elem0 ... elemN)', where each ELEM is from the corresponding STREAM. `stream-for-each' stops when it reaches the end of any of the STREAMs. -- Function: stream-map proc stream0 ... streamN Return a new stream which is the results of applying PROC to the elements of the given STREAMs. Each call is `(PROC elem0 ... elemN)', where each ELEM is from the corresponding STREAM. The new stream ends when the end of any of the given STREAMs is reached. --=-=-= Content-Disposition: attachment; filename=streams.scm.for-each.diff --- streams.scm.~1.8.~ 2003-04-07 08:05:00.000000000 +1000 +++ streams.scm 2004-09-04 12:22:07.000000000 +1000 @@ -190,7 +190,7 @@ (if (not (or-map stream-null? streams)) (begin (apply f (map stream-car streams)) - (stream-for-each-one f (map stream-cdr streams))))) + (stream-for-each-many f (map stream-cdr streams))))) (define (stream-map f stream . rest) "Returns a newly allocated stream, each element being the result of --=-=-= Content-Disposition: attachment; filename=streams.test ;;;; streams.test --- test Guile ice-9 streams module -*- scheme -*- ;;;; ;;;; Copyright (C) 2004 Free Software Foundation, Inc. ;;;; ;;;; This program is free software; you can redistribute it and/or modify ;;;; it under the terms of the GNU General Public License as published by ;;;; the Free Software Foundation; either version 2, or (at your option) ;;;; any later version. ;;;; ;;;; This program is distributed in the hope that it will be useful, ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;;; GNU General Public License for more details. ;;;; ;;;; You should have received a copy of the GNU General Public License ;;;; along with this software; see the file COPYING. If not, write to ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ;;;; Boston, MA 02111-1307 USA (define-module (test-suite test-streams) :use-module (test-suite lib) :use-module (ice-9 streams)) ;;; ;;; stream-for-each ;;; (with-test-prefix "stream-for-each" (with-test-prefix "1 streams" (pass-if "empty" (let ((lst '())) (stream-for-each (lambda (x) (set! lst (cons x lst))) (list->stream '())) (equal? '() lst))) (pass-if "123" (let ((lst '())) (stream-for-each (lambda (x) (set! lst (cons x lst))) (list->stream '(1 2 3))) (equal? '(3 2 1) lst)))) (with-test-prefix "2 streams" (pass-if "empty empty" (let ((lst '())) (stream-for-each (lambda (x y) (set! lst (cons* x y lst))) (list->stream '()) (list->stream '())) (equal? '() lst))) (pass-if "123 456" (let ((lst '())) (stream-for-each (lambda (x y) (set! lst (cons* x y lst))) (list->stream '(1 2 3)) (list->stream '(4 5 6))) (equal? '(3 6 2 5 1 4) lst))) (pass-if "12 456" (let ((lst '())) (stream-for-each (lambda (x y) (set! lst (cons* x y lst))) (list->stream '(1 2)) (list->stream '(4 5 6))) (equal? '(2 5 1 4) lst))) (pass-if "123 45" (let ((lst '())) (stream-for-each (lambda (x y) (set! lst (cons* x y lst))) (list->stream '(1 2 3)) (list->stream '(4 5))) (equal? '(2 5 1 4) lst))))) --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://lists.gnu.org/mailman/listinfo/guile-devel --=-=-=--