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 17:50:12

Mac Mason schrieb:
>
>; >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.
>

you can use Stream.from , it constructs a stream from a function.
you read the first string with maybe scanf, then use Stream.from to
build the stream.
Something like (throws an exception on invalid input):

let make_stream in_channel =
let read_func (cnt:int) =
try
let nr = Scanf.fscanf in_channel "%d" (fun n -> n) in
Some(nr)
with
End_of_file -> None
in
Stream.from read_func;;

cheers,
Michael

__._,_.___
.

__,_._,___
[1]

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