caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] copying Streams
@ 2001-07-10  3:44 John Erickson
  2001-07-10  8:44 ` Daniel de Rauglaudre
  2001-07-10  9:03 ` Pierre.Boulet
  0 siblings, 2 replies; 3+ messages in thread
From: John Erickson @ 2001-07-10  3:44 UTC (permalink / raw)
  To: caml-list

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?

John
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] copying Streams
  2001-07-10  3:44 [Caml-list] copying Streams John Erickson
@ 2001-07-10  8:44 ` Daniel de Rauglaudre
  2001-07-10  9:03 ` Pierre.Boulet
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel de Rauglaudre @ 2001-07-10  8:44 UTC (permalink / raw)
  To: John Erickson; +Cc: caml-list

Hi,

On Mon, Jul 09, 2001 at 10:44:16PM -0500, John Erickson wrote:

> 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?

No, but in Camlp4, you have a library of functional streams Fstream
and syntax for functional streams and functional parsers pa_fstream.cmo.
See Camlp4 reference manual.
     http://caml.inria.fr/camlp4/manual/
     http://caml.inria.fr/camlp4/manual/manual007.html#toc30

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] copying Streams
  2001-07-10  3:44 [Caml-list] copying Streams John Erickson
  2001-07-10  8:44 ` Daniel de Rauglaudre
@ 2001-07-10  9:03 ` Pierre.Boulet
  1 sibling, 0 replies; 3+ messages in thread
From: Pierre.Boulet @ 2001-07-10  9:03 UTC (permalink / raw)
  To: John Erickson; +Cc: caml-list

> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-07-10  9:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-10  3:44 [Caml-list] copying Streams John Erickson
2001-07-10  8:44 ` Daniel de Rauglaudre
2001-07-10  9:03 ` Pierre.Boulet

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).