caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre.Boulet@lifl.fr (Pierre.Boulet)
To: John Erickson <jderick@cs.utexas.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] copying Streams
Date: 10 Jul 2001 11:03:22 +0200	[thread overview]
Message-ID: <vtu1ynp6tr9.fsf@gala.lifl.fr> (raw)
In-Reply-To: John Erickson's message of "Mon, 9 Jul 2001 22:44:16 -0500"

> 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


      parent reply	other threads:[~2001-07-10  9:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-10  3:44 John Erickson
2001-07-10  8:44 ` Daniel de Rauglaudre
2001-07-10  9:03 ` Pierre.Boulet [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=vtu1ynp6tr9.fsf@gala.lifl.fr \
    --to=pierre.boulet@lifl.fr \
    --cc=caml-list@inria.fr \
    --cc=jderick@cs.utexas.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).