Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Should be INSIDE STANDARD-LIB: Hashtbl.keys
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-21 (09:14)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] Should be INSIDE STANDARD-LIB: Hashtbl.keys
On Wed, 21 Apr 2004, Oliver Bandel wrote:

>       Hashtbl.keys <hash>  returns a list of all keys of the
>       hash "hash", that exists and therefore have values bound
>       to it. If a key has several bindings, it will occur only
>       once in this list.

For that purpose, it is possible instead of a (key, data) Hashtbl.t to use
a (key, data list ref) Hashtbl.t:

let add tbl key data =
  let r =
    try Hashtbl.find tbl key
    with Not_found ->
      let r = ref [] in
      Hashtbl.add tbl key r;
      r in
  r := data :: !r

let fold f tbl init =
  Hashtbl.fold (fun key r accu -> f key (List.hd !r) accu) tbl init

let keys tbl =
  fold (fun key _ accu -> key :: accu) tbl []


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: