Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] ocaml and large development projects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] Reading a file
Siegfried Gonzi schrieb am Donnerstag, den 22. Mai 2003:
> Thanks for this version. Without any insult, it works well and takes for
> this legendary file now 25 seconds, though, I find the version ugly. The
> Clean, Bigloo, Fortran 95 and C++ version more or less takes the same time.

There is always room for making things faster or shorter: it depends on
what you prefer.

If you want to have an even faster version, you could use float arrays
instead of float lists. E.g. instead of this line in the previous
solution:

  | Some line -> loop (List.map my_frob (split line del) :: acc)

Use this:

  | Some line ->
      let str_ar = Array.of_list (split line del) in
      loop (Array.map my_frob str_ar :: acc)

This is probably what you really need anyway.

If you want to have a short version, which is still pretty fast, use this:

---------------------------------------------------------------------------
let frob userval = function
  | "n/a" | "nil" -> userval
  | s -> float_of_string s
;;

let extract_floats file del nan_proxy =
  let rex = Str.regexp_string (String.make 1 del) in
  let my_frob = frob nan_proxy in
  let acc_ref = ref [] in
  (try
    while true do
      let line = input_line file in
      let str_ar = Array.of_list (Str.split rex line) in
      acc_ref := Array.map my_frob str_ar :: !acc_ref
    done
  with End_of_file -> ());
  List.rev !acc_ref
;;

let f = open_in "/home/gonzi/test.txt";;
let erg2 = extract_floats f ',' (-1.0);;
let rows = List.length erg2;;
rows;;
---------------------------------------------------------------------------

> But there remains my requirement for a better OCaml solution.

Better = faster or smaller...?

> 1) Convert the string-line to a character-list: [x\\x<-: string-line]

This would be _very_ inefficient.

> This, oh dear Watson is what I mean with elegance. No need of string
> indexing counting or other error prone stuff.

Use regular expressions: fast enough and very short.

> So, this must also be possible in OCaml - or must not?

You cannot just take one solution of one language and assume that it
is equally efficient or elegant in another language. This also heavily
depends on availability and functionality of libraries. E.g. there is no
"take_while" or "drop_while" in the OCaml standard library - you'd have
to write them yourself.

Regards,
Markus Mottl

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@oefai.at

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners