English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] printable digest strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-05-04 (14:50)
From: Brian Rogoff <bpr@b...>
Subject: Re: [Caml-list] printable digest strings
On Fri, 4 May 2001, Miles Egan wrote:
> This actually brings me to my next question.  There doesn't seem to be a
> String.map or String.iter function in the standard library, although I can
> imagine they might also be handy.  Am I looking in the wrong place?

Nope, they're not there. You could use the compiler source, and steal the
corresponding functions from arrays, since OCaml does the right thing and 
has array-like strings (as opposed to say, Erlang and Haskell). Here's the 
touch up for those two

let iter f s =
  for i = 0 to String.length s - 1 do f(String.unsafe_get s i) done

let map f s =
  let l = String.length s in
  if l = 0 then "" else begin
    let r = String.create l in
    for i = 1 to l - 1 do
      String.unsafe_set r i (f(String.unsafe_get s i))

You can now (OCaml 3.01) make your own String module with these functions
and include the native String module to get what you want. 

This is another one of those cases where some overloading might be nice,
since IMO there is not much point in distinguishing all of the different 
kinds of random access with .[], .(), .{}. 

-- Brian

To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr