List Info

Thread: Re: "ocaml_beginners"::[] Turning a file into a space-delimited stream?




Re: "ocaml_beginners"::[] Turning a file into a space-delimited stream?
user name
2007-01-26 16:33:18

On Fri, 26 Jan 2007, Mac Mason wrote:

> I've got a file that has many, many whitespace-delimited integers (and
>; one whitespace-delimited string, as the very first element).
>
> I'm looking for a clean way to read them in one at a time; what I'd
> really like is a stream (or an Enum, I suppose) that's constructed
> from these values. I can put this together as a combination of
> Std.input_all, Str.split, and so on, but it's really awful and I don't
> know much about the tail-recursive properties of those functions (like
> I said: many, many integers). It seems like I should be able to
> (ab)use the stream syntax or module to do this for me.

let buf = Buffer.create 64

let next ic =
let rec get_chunk ic in_chunk =
match input_char ic with
| ' ' | 't' | 'n' | 'r' ->
if in_chunk then
let chunk = Buffer.contents buf in
Buffer.clear buf;
Some chunk
else
get_chunk ic false
| c -> Buffer.add_char buf c; get_chunk ic true
in
try get_chunk ic false with End_of_file -> None
;;

let stream_from_channel ic =
Stream.from (fun _ -> next ic)
;;

This should give you a stream of whitespace delimited chunks (adjust the
match patterns if I missed something, or to filter out non-printable
characters or whatever). You can improve the performance by roading a
hunk of characters in at a time and pulling the chars from your buffer,
refilling when you hit the end, but this should give the general flavor.

I'm sure you can do something similar with the camlp4 streams and parsers
if you'd prefer that style (looks cleaner, some things are easier, some
harder).

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy."

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )