caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] stream conversion
@ 2003-12-09  7:56 Dustin Sallings
  2003-12-09 12:08 ` Remi Vanicat
  2003-12-10  1:09 ` Nicolas Cannasse
  0 siblings, 2 replies; 10+ messages in thread
From: Dustin Sallings @ 2003-12-09  7:56 UTC (permalink / raw)
  To: Caml Mailing List


	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?

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

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


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

* Re: [Caml-list] stream conversion
  2003-12-09  7:56 [Caml-list] stream conversion Dustin Sallings
@ 2003-12-09 12:08 ` Remi Vanicat
  2003-12-09 16:58   ` Jean-Baptiste Rouquier
  2003-12-09 19:15   ` Dustin Sallings
  2003-12-10  1:09 ` Nicolas Cannasse
  1 sibling, 2 replies; 10+ messages in thread
From: Remi Vanicat @ 2003-12-09 12:08 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] stream conversion
  2003-12-09 12:08 ` Remi Vanicat
@ 2003-12-09 16:58   ` Jean-Baptiste Rouquier
  2003-12-09 17:25     ` brogoff
  2003-12-09 19:13     ` Dustin Sallings
  2003-12-09 19:15   ` Dustin Sallings
  1 sibling, 2 replies; 10+ messages in thread
From: Jean-Baptiste Rouquier @ 2003-12-09 16:58 UTC (permalink / raw)
  To: caml-list

Dustin Sallings <dustin@spy.net> wrote:

>I have a need to convert a ``string stream'' to a ``char stream.''
>
Small is beautiful !
#load "camlp4o.cma";;
let rec convert = parser
  | [< 'string; stream >] -> [<Stream.of_string string; convert stream>]
  | [<>] -> [<>]

(* That's it... However this function compiles but doesn't work.*)
(* Can anybody explain the following problem: *)
let foo = [< Stream.of_string "bar"; Stream.of_string "quz">]
let _ = Stream.next foo
(* Exception: Failure "illegal stream concatenation". *)




(* So I have to implement my own (awful : skip it!) concatenation 
function : *)
let concat s s' =
  let stream_next =
    let rec f = ref (fun _ ->
      try Some (Stream.next s)
      with Stream.Failure ->
    Printf.printf "redefining f\n%!";
    f := (fun _ ->
        (try Some (Stream.next s')
         with Stream.Failure -> None));
    !f 0) in
    fun x -> !f x
  in Stream.from stream_next


(* And then slightly rewrite my function : *)

let rec convert = parser
  | [< 'string; stream >] -> concat (Stream.of_string string) (convert 
stream)
  | [<>] -> [<>]



(*let's test it*)
let foo = convert (Stream.of_list ["foo"; "bar"; ""; "qux"])

let _ = Stream.next foo;;
  (* - : char = 'f' *)
let _ = Stream.next foo;;
  (* - : char = 'o' *)
let _ = Stream.next foo;;
  (* - : char = 'o' *)
let _ = Stream.next foo;;
  (* redefining f *)
  (* - : char = 'b' *)
let _ = Stream.next foo;;
  (* - : char = 'a' *)
let _ = Stream.next foo;;
  (* - : char = 'r' *)
let _ = Stream.next foo;;
  (* redefining f *)
  (* redefining f *)
  (* - : char = 'q' *)
let _ = Stream.next foo;;
  (* - : char = 'u' *)
let _ = Stream.next foo;;
  (* - : char = 'x' *)
let _ = Stream.next foo;;
  (* redefining f *)
  (* Exception: Stream.Failure. *)


Except the concatenation problem, I think camlp4 makes it much clearer, 
and it works with empty strings.

Jean-Baptiste Rouquier.

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


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

* Re: [Caml-list] stream conversion
  2003-12-09 16:58   ` Jean-Baptiste Rouquier
@ 2003-12-09 17:25     ` brogoff
  2003-12-09 17:40       ` Jean-Baptiste Rouquier
  2003-12-09 18:30       ` Tim Freeman
  2003-12-09 19:13     ` Dustin Sallings
  1 sibling, 2 replies; 10+ messages in thread
From: brogoff @ 2003-12-09 17:25 UTC (permalink / raw)
  To: Jean-Baptiste Rouquier; +Cc: caml-list

The documentation in the Stream library for the stream builders (you use
Stream.of_string) states :

   Warning: these functions create streams with fast access; it is illegal to
   mix them with streams built with [< >]; would raise Failure  when
   accessing such mixed streams.

and you build such streams by stream concatenation with your convert function
and when you make foo.

Yes, this is very annoying.

-- Brian



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


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

* Re: [Caml-list] stream conversion
  2003-12-09 17:25     ` brogoff
@ 2003-12-09 17:40       ` Jean-Baptiste Rouquier
  2003-12-09 18:30       ` Tim Freeman
  1 sibling, 0 replies; 10+ messages in thread
From: Jean-Baptiste Rouquier @ 2003-12-09 17:40 UTC (permalink / raw)
  To: caml-list

brogoff@speakeasy.net wrote:

>The documentation in the Stream library for the stream builders (you use
>Stream.of_string) states :
>  
>
Ah yes, I remember reading that one year ago, sorry.
Here is a more readable solution :

#load "camlp4o.cma";;
let stream_of_string s =
  let len = String.length s in
  let rec stream pos =
    if pos < len then [< 's.[pos]; stream (pos+1) >] else [<>] in
  stream 0

let rec convert = parser
  | [< 'string; stream >] -> [<stream_of_string string; convert stream>]
  | [<>] -> [<>]

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


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

* Re: [Caml-list] stream conversion
  2003-12-09 17:25     ` brogoff
  2003-12-09 17:40       ` Jean-Baptiste Rouquier
@ 2003-12-09 18:30       ` Tim Freeman
  2003-12-16 22:42         ` Damien Doligez
  1 sibling, 1 reply; 10+ messages in thread
From: Tim Freeman @ 2003-12-09 18:30 UTC (permalink / raw)
  To: brogoff; +Cc: jean-baptiste.rouquier, caml-list

From: brogoff@speakeasy.net
>The documentation in the Stream library for the stream builders (you use
>Stream.of_string) states :
>
>   Warning: these functions create streams with fast access; it is illegal to
>   mix them with streams built with [< >]; would raise Failure  when
>   accessing such mixed streams.
>
>and you build such streams by stream concatenation with your convert function
>and when you make foo.

There's a fix for this bug at http://www.fungible.com/stream_fixed.ml
and http://www.fungible.com/stream_fixed.mli.

It's bug 1284 in the OCAML bug-tracking system.

According to the benchmark given in the comments at the end of
http://www.fungible.com/stream_fixed.ml, my version is essentially the
same speed as the original.

Unlike the original version, it does not do type-unsafe efficiency
hacks.

The bug database at http://caml.inria.fr/bin/caml-bugs documents that
bug 1284 has been ignored without comment as of 9 Dec 2003.  My code
is a clean solution to a problem that has been reported repeatedly, so
I find that frustrating.  This looks to me like a clear signal that I
shouldn't bother sending them any other bugfixes.

-- 
Tim Freeman                                                  tim@fungible.com
I xeroxed a mirror. Now I have an extra xerox machine.       -- Steven Wright


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


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

* Re: [Caml-list] stream conversion
  2003-12-09 16:58   ` Jean-Baptiste Rouquier
  2003-12-09 17:25     ` brogoff
@ 2003-12-09 19:13     ` Dustin Sallings
  1 sibling, 0 replies; 10+ messages in thread
From: Dustin Sallings @ 2003-12-09 19:13 UTC (permalink / raw)
  To: Jean-Baptiste Rouquier; +Cc: caml-list


On Dec 9, 2003, at 8:58, Jean-Baptiste Rouquier wrote:

> Small is beautiful !
> #load "camlp4o.cma";;

	I agree, and that's exactly why I was asking.  campl4 is one of those 
things I've kind of heard of, but never had an idea of what it actually 
did.  I also saw references to [<, but couldn't figure out where that 
was defined.  Thanks.

-- 
Dustin Sallings

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


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

* Re: [Caml-list] stream conversion
  2003-12-09 12:08 ` Remi Vanicat
  2003-12-09 16:58   ` Jean-Baptiste Rouquier
@ 2003-12-09 19:15   ` Dustin Sallings
  1 sibling, 0 replies; 10+ messages in thread
From: Dustin Sallings @ 2003-12-09 19:15 UTC (permalink / raw)
  To: Remi Vanicat; +Cc: caml-list


On Dec 9, 2003, at 4:08, Remi Vanicat wrote:

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

	I think I considered that, but didn't explore that direction for some 
reason.  I'll give that a shot.  Overall, I'm looking for a bit less 
duplication.  It just seems like the whole thing should be smaller, so 
I assume there's a better way.

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

	Well, I believe that I will have no empty streams in my use case, but 
that's no excuse.

	Thanks for the pointers.

-- 
Dustin Sallings

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


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

* Re: [Caml-list] stream conversion
  2003-12-09  7:56 [Caml-list] stream conversion Dustin Sallings
  2003-12-09 12:08 ` Remi Vanicat
@ 2003-12-10  1:09 ` Nicolas Cannasse
  1 sibling, 0 replies; 10+ messages in thread
From: Nicolas Cannasse @ 2003-12-10  1:09 UTC (permalink / raw)
  To: Caml Mailing List, Dustin Sallings

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

BTW, I don't know if you're using streams in a parser, but if you're using
them only as enumerations, you should consider using the Enum module instead
( Enum is part of the ExtLib : http://ocaml-lib.sourceforge.net )

One example of conversion of a string enum to a char enum :

open ExtString
let to_chars e =
    Enum.concat (Enum.map String.enum e)

I don't know about the Stream implementation, but Enum are purely
functional. That means there is no temporary "char list" or "char array"
created anywhere, only a int ref in a closure actually.

Nicolas Cannasse

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


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

* Re: [Caml-list] stream conversion
  2003-12-09 18:30       ` Tim Freeman
@ 2003-12-16 22:42         ` Damien Doligez
  0 siblings, 0 replies; 10+ messages in thread
From: Damien Doligez @ 2003-12-16 22:42 UTC (permalink / raw)
  To: caml-list

On Tuesday, December 9, 2003, at 07:30 PM, Tim Freeman wrote:

> It's bug 1284 in the OCAML bug-tracking system.
[...]
> The bug database at http://caml.inria.fr/bin/caml-bugs documents that
> bug 1284 has been ignored without comment as of 9 Dec 2003.  My code
> is a clean solution to a problem that has been reported repeatedly, so
> I find that frustrating.  This looks to me like a clear signal that I
> shouldn't bother sending them any other bugfixes.

Unless the status has changed since 9 Dec, it is *not* ignored.
It's classified as a feature wish, which means that we will implement
it when we get around to it.  Please understand that work on camlp4
is going rather slowly since we lost Daniel.

We are very grateful for all bug fixes proposed by users, regardless
of whether we choose to use them or not.

-- Damien

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


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

end of thread, other threads:[~2003-12-16 22:41 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-09  7:56 [Caml-list] stream conversion Dustin Sallings
2003-12-09 12:08 ` Remi Vanicat
2003-12-09 16:58   ` Jean-Baptiste Rouquier
2003-12-09 17:25     ` brogoff
2003-12-09 17:40       ` Jean-Baptiste Rouquier
2003-12-09 18:30       ` Tim Freeman
2003-12-16 22:42         ` Damien Doligez
2003-12-09 19:13     ` Dustin Sallings
2003-12-09 19:15   ` Dustin Sallings
2003-12-10  1:09 ` Nicolas Cannasse

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