Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] stream conversion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi Vanicat <remi.vanicat@l...>
Subject: Re: [Caml-list] stream conversion
Dustin Sallings <dustin@spy.net> writes:

> 	I have a need to convert a ``string stream'' to a ``char
> stream.''  It's a little unclear to me how I'd accomplish this.  This
> is the best I've been able to come up with so far, but I have to
> imagine there's a prettier solution:
>
> let stream_convert source =
>      let chunk = ref (Stream.of_string (Stream.next source)) in
>      Stream.from (fun x ->
>          try
>              Stream.empty !chunk;
>              try
>                  Stream.empty source;
>                  None
>              with Stream.Failure ->
>                  chunk := (Stream.of_string (Stream.next source));
>                  Some (Stream.next !chunk)
>          with Stream.Failure ->
>              Some (Stream.next !chunk)
>      )
> ;;
>
> 	Any suggestions?

why do you use Stream.empty ? it would be more natural to use only
Stream.next. As in :
let stream_convert source =
  let chunk = ref (Stream.of_string (Stream.next source)) in
  Stream.from (fun x ->
    try 
      Some (Stream.next !chunk)
    with
      Stream.Failure ->
       (* code needed when the chunk is empty *)
    )


By the way, your code make the assumption that no string in the source
stream are empty (look at what would happen in such case).


-- 
Rémi Vanicat

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners