Data.HashTable and duplicate keys

Hello I recently noticed that Data.HashTable silently accepts duplicate keys and keeps all the values. This is very counterintuitive from the interface and makes updating a hashtable quite tedious (lookup + delete + insert). Mentioning the feature in the documentation and providing an update function would be very nice. e.g. update :: HashTable k v -> k -> (v -> v) -> IO Bool or update :: HashTable k v -> k -> (v -> (v, w)) -> IO (Maybe w) with the result marking whether the key was present in the hashtable. On the other hand, if it is a bug I hope it will be solved quickly. The behaviour can be demonstrated with main = do ht <- new (==) hashInt insert ht 1 1 insert ht 1 2 toList ht >>= print - Einar Karttunen

On Mon, 27 Sep 2004, Einar Karttunen wrote:
I recently noticed that Data.HashTable silently accepts duplicate keys and keeps all the values. This is very counterintuitive from the interface and makes updating a hashtable quite tedious (lookup + delete + insert).
OCaml's Hashtbl also works this way. It makes sense if you think of a hash table as a speedier drop-in replacement for an association list: Data.HashTable.insert behaves like consing a new (key,val) pair onto the front of an association list, and Data.HashTable.delete and Data.HashTable.lookup behave like the corresponding list functions. I agree 100% that it should be documented, of course. However, playing around with HashTable just now, I noticed the following (GHCi): Prelude> hash <- Data.HashTable.fromList id [(1,'a'),(1,'b')] Prelude> Data.HashTable.lookup hash 1 >>= print Just 'b' This is clearly wrong, and makes me think that Data.HashTable's behavior given identical keys wasn't well thought out. I'm in favor of plagiarizing OCaml's Hashtbl interface, which has the update function (called replace) as well as other useful operations like copy and find_all. -- Ben
participants (2)
-
Ben Rudiak-Gould
-
Einar Karttunen