From: Jean-Baptiste Rouquier <jean-baptiste.rouquier@ens-lyon.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] stream conversion
Date: Tue, 09 Dec 2003 17:58:44 +0100 [thread overview]
Message-ID: <3FD5FF44.3000003@ens-lyon.org> (raw)
In-Reply-To: <87brqi8a8i.dlv@wanadoo.fr>
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
next prev parent reply other threads:[~2003-12-09 16:58 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-09 7:56 Dustin Sallings
2003-12-09 12:08 ` Remi Vanicat
2003-12-09 16:58 ` Jean-Baptiste Rouquier [this message]
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
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=3FD5FF44.3000003@ens-lyon.org \
--to=jean-baptiste.rouquier@ens-lyon.org \
--cc=caml-list@inria.fr \
/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