caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: kirillkh <kirillkh@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] best and fastest way to read lines from a file?
Date: Tue, 2 Oct 2007 22:49:41 +0200	[thread overview]
Message-ID: <e2d02be30710021349m78417caakb3d1afd0714f1ebe@mail.gmail.com> (raw)
In-Reply-To: <95513600710021323u762efd5k5ee6bdd03d7cc37@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2028 bytes --]

2007/10/2, Olivier Andrieu <oandrieu@gmail.com>:
>
> On 10/2/07, kirillkh <kirillkh@gmail.com> wrote:
> > OK, so I'll give up the parsing/buffering part and only leave efficient
> > exception handling. This should leave the user free to do anything with
> it,
> > but prevent performance pitfalls. The following is based on Mattias
> > Engdegard's code:
> >
> > (* I couldn't figure out, how to declare a polymorphic exception
> properly *)
> > exception Done of 'a
> >
> >  let fold_file (file: in_channel)
> >               (read_func: in_channel->'a)
> >               (elem_func: 'a->'b->'b)
> >               (seed: 'b) =
> >   let rec loop prev_val =
> >     let input =
> >       try read_func file
> >       with End_of_file -> raise (Done prev_val)
> >     in
> >       let combined_val = elem_func input prev_val in
> >       loop combined_val
> >   in
> >     try loop seed with Done x -> x
> >
> > And the usage for line counting:
> >
> > let line_count filename =
> >    let f = open_in filename in
> >    let counter _ count = count + 1 in
> >    fold_file f readline counter 0
> >
> > Since it's library code, we can tolerate the little annoyance of the
> second
> > try-catch. As far as I can tell, this code has the same performance
> > characteristics as yours: no consing + tail recursion. Any other
> problems
> > with it?
>
> well apart from the fact that you cannot have "polymorphic exceptions"
> in OCaml, this kind of code is IMHO much more natural with an
> imperative loop instead of a functional one:
>
>
> let fold_file read chan f init =
>   let acc = ref init in
>   begin
>     try while true do
>       let d = read chan in
>       acc := f d !acc
>     done
>     with End_of_file -> ()
>   end ;
>   !acc
>
> --
>   Olivier
>

A little weird to see such inherently functional construct as fold
implemented imperatively. But it's fine with me, as long as it does the job.
I wonder, though, how would the performance of a line counter based on your
code compare with the one suggested by Brian.

[-- Attachment #2: Type: text/html, Size: 3191 bytes --]

  reply	other threads:[~2007-10-02 20:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-01 21:27 YC
2007-10-01 21:55 ` [Caml-list] " Daniel Bünzli
2007-10-01 22:29   ` YC
2007-10-01 21:55 ` Olivier Roussel
2007-10-02 12:39   ` Mattias Engdegård
2007-10-02 12:56     ` Brian Hurt
2007-10-02 16:15       ` kirillkh
2007-10-02 17:10         ` verlyck, Bruno.Verlyck
2007-10-02 18:02         ` kirillkh
2007-10-02 19:35           ` skaller
2007-10-02 21:05             ` kirillkh
2007-10-02 21:07               ` Jon Harrop
2007-10-02 20:23           ` Olivier Andrieu
2007-10-02 20:49             ` kirillkh [this message]
2007-10-02 21:10               ` Jon Harrop
2007-10-02 21:15               ` David Allsopp
2007-10-02 22:23                 ` skaller

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=e2d02be30710021349m78417caakb3d1afd0714f1ebe@mail.gmail.com \
    --to=kirillkh@gmail.com \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).