Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Hashtbl.keys
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Hashtbl.keys
> Well... and Hashtbl.iter seems to work on *all* antries in the
> *whole* hash-table, that is: A Hastbl.find_all  for each of the
> keys in that table.
> At least it is, how I understand the decription in the manual.
> 
> Well, but Hashtbl.iter is used for unit-functions.
> But I need bool-functions, and maybe other tyopes
> too.

See Hashtbl.fold, which supports traversing all entries of a hashtable
and build a result of any type.  For instance:

> So, Hashtbl.keys would have the following type:
> 
> ('a, 'b) t -> key 'a list

let keys h = Hashtbl.fold (fun key data accu -> key :: accu) h []

> Wouldn't it a good idea to add such a
> function "get all keys of a hashtable"
> into the hashtbl-module? (And a "get all values..." to?)

There would be disagreement as what the return type of these functions
should be: lists, sets, etc.  But you can easily write them with
Hashtbl.fold.

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