From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA19928; Tue, 10 Jul 2001 11:03:35 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA19915 for ; Tue, 10 Jul 2001 11:03:34 +0200 (MET DST) Received: from malonne.lifl.fr (malonne.lifl.fr [134.206.10.29]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f6A93YX20413 for ; Tue, 10 Jul 2001 11:03:34 +0200 (MET DST) Received: from gala.lifl.fr (gala.lifl.fr [134.206.10.216]) by malonne.lifl.fr (8.9.3/jtpda-5.3.3) with ESMTP id LAA01629 ; Tue, 10 Jul 2001 11:03:23 +0200 (MET DST) Received: from (boulet@localhost) by gala.lifl.fr (8.9.3/jtpda-5.3.3) id LAA17656 ; Tue, 10 Jul 2001 11:03:22 +0200 To: John Erickson Cc: caml-list@inria.fr Subject: Re: [Caml-list] copying Streams References: <20010709224416.B11837@mothra.cs.utexas.edu> Mime-Version: 1.0 (generated by tm-edit 7.108) Content-Type: text/plain; charset=US-ASCII From: Pierre.Boulet@lifl.fr (Pierre.Boulet) Date: 10 Jul 2001 11:03:22 +0200 In-Reply-To: John Erickson's message of "Mon, 9 Jul 2001 22:44:16 -0500" Message-ID: X-Mailer: Gnus v5.6.45/XEmacs 21.1 - "Bryce Canyon" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Is there any way to copy a Stream (from the standard library) > in such a way that the original stream is not modified when the > copy is used and vice versa? I have not managed yet to do what you want but I may have a workaround: here is a function that builds two streams from another one such that the two produced streams work together to only remove elements form the source stream when both have read it. They thus appear as two independent streams containing the same elements. let stream_get_nth n s = let i = (n+1)-(Stream.count s) in try List.nth (Stream.npeek i s) (i-1) with Failure "nth" -> raise Stream.Failure let stream_duplicate s = let start_count = Stream.count s in let count1 = ref start_count and count2 = ref start_count in let get c1 c2 n = let n' = n + start_count in if n' < !c1 then raise Stream.Failure else begin incr c1; if n' >= !c2 then try Some (stream_get_nth n' s) with Stream.Failure -> None else try Some (Stream.next s) with Stream.Failure -> None end in let get1 n = get count1 count2 n and get2 n = get count2 count1 n in (Stream.from get1), (Stream.from get2) -- Pierre.Boulet@lifl.fr - http://www.lifl.fr/~boulet In theory, practice and theory are the same, but in practice they are different. -- Larry McVoy ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr