List Info

Thread: Re: "ocaml_beginners"::[] I/O streams (fwd)




Re: "ocaml_beginners"::[] I/O streams (fwd)
country flaguser name
United States
2008-06-06 13:28:57

On Fri, 6 Jun 2008, citromatik wrote:

>
>
> Martin Jambon wrote:
> >
> >
> > Here is some not-so-old code that I dug from a program.
> > I don't have much time right now to clean it up, so I'll leave it as an
> > exercise
> >
> > You need to know a few things:
> >
> > - it uses the micmatch syntax extension for testing the type of line (RE
> > ...). You really don't have to.
> > - the function takes in in_channel and returns a function (loop). This
>; > function returns Some (name, seq) or None if we are at the end of the
> > channel.
> > - the channel is not closed at the end (the code that is responsible for
> > opening it should also be responsible for closing it)
> >
> >
> >
>
> Thank you very much for your example.
> Here is the code I ended with (essentially the same you posted, but with a
> couple of changes):
>
> let get_seq ?(maxi = max_int) ic =
> let seqid = ref "" in
> let buf = Buffer.create 4096 in
> let counter = ref 0 in
> let rec loop _ =
> try
> let s = input_line ic in
> if String.contains s '>' then
>; let name = !seqid in
> let seq = Buffer.contents buf in
> seqid := s;
> Buffer.clear buf;
>; if seq <> "&quot; then
>; (incr counter;
> if !counter mod 10_000 = 0 then
>; Printf.printf "processed %i sequencesn%!" !counter;
> if !counter < maxi then
>; Some (name, seq)
>; else None)
&gt; else
>; loop 0
> else
>; (Buffer.add_string buf s;
> loop 0)
> with End_of_file ->
&gt; let name = !seqid in
> let seq = Buffer.contents buf in
> seqid := "&quot;;
> Buffer.clear buf;
>; if seq <> "&quot; then
>; (incr counter;
> Some (name, seq))
&gt; else
>; (Printf.printf "total: %i sequencesn%!" !counter;
> None) in
> loop
>; ; ;
>
> I had to change the code that uses micmatch because it seems that this
>; library doesn't work on version 3.10 of Ocaml (the one I'm using), is this
>; correct?

Yes. I've done maybe 2/3 of the work required to port it to 3.10.
Camlp4 3.10 is largely incompatible with Camlp4 3.09, and there's no reference
manual, so it takes time.

> it is a pity because it would be very useful for me, maybe I should
&gt; downgrade to 3.09. Also, it doesn't appear in the list of available packages
> in GODI.

Right, you'll see it only if you use 3.09.

> This is an example of usage using this function in a stream:
>
> let print_sequences_length filename =
> let fh = open_in filename in
> let () = Stream.iter
> ( fun (h,s) -> Printf.printf "%s => %dn" h (String.length s) )
> (Stream.from (get_seq fh)) in
> close_in fh;;
>;
> Again, thanks a lot for the help!

You're welcome. I'm glad my old dead code could still be useful...

Martin

--
http: //wink.com/profile/mjambon
http: //mjambon.com

__._,_.___
.

__,_._,___
[1]

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