Version franēaise
Home     About     Download     Resources     Contact us    
Browse thread
Locally-polymorphic exceptions [was: folding over a file]
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Re: Locally-polymorphic exceptions [was: folding over a file]
Hi,

> 2007/10/3, oleg@pobox.com <oleg@pobox.com>:
>
> exception Done of 'a
>
>  let fold_file (file: in_channel)
>               (read_func: in_channel->'a)
>               (elem_func: 'a->'b->'b)
>               (seed: 'b) =
> [...]

Personnally, I don't like exceptions because they generally control too
much part of code. I often practice things like:

  match try Some (...) with [ Exception -> None ] with
  [ Some v -> blabla
  | None -> blublu ]

I would write your function like this:

  value fold_file (file : in_channel) (read_func : in_channel -> 'a)
    (elem_func : 'a -> 'b -> 'b) (seed : 'b)
  =
    let rec loop prev_val =
      match try Some (read_func file) with [ End_of_file -> None ] with
      [ Some input ->
          let combined_val = elem_func input prev_val in
          loop combined_val
      | None -> prev_val ]
    in
    loop seed
  ;

-- 
Daniel de Rauglaudre
http://pauillac.inria.fr/~ddr/