Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] String.map => Question to the OCaml-team
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Correnson_Loïc <Loic.Correnson@t...>
Subject: Re: [Caml-list] String.map => Question to the OCaml-team
Have also a look at Array.iteri :
It allows to not use a for, nor an extra ref.
It shall be possible to have in lib:

String.iter : (char -> unit) -> string -> unit
String.iteri : (int ->char -> unit) -> string -> unit

Then:
let map f s = let t = String.copy s in ( String.iteri (fun i c -> t.[i] <- f
c) s ; t )

However, without extra libs nor for, but with a tail-rec function :
let map f s =
    let rec iteri i n t = if i<n then (t.[i] <- f t.[i] ; iteri (succ i) n
t) else t in
    iteri 0 (String.length s) (String.copy s)

Don't known what about performances.
    LC.

-------------------
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