How to use assocs with HashMap in Haskell

Currently I'm trying to use the Map's assocs method, but unable to figure out how to get it to work for a HashMap. For a regular Map the following works just fine.

import qualified Data.Map as M
test = M.fromList [("a", 1), ("b", 2)]
M.assocs test

However when I try the same thing with a HashMap it doesn't work. I tried several variation on the import all fail with different errors. Oddly however most other functions that work on maps work just fine with the below import, for example I have no trouble using M.lookup.

import qualified Data.HashMap.Lazy as M
test = M.fromList [("a", 1), ("b", 2)]
M.assocs test

In case it is useful the above code gives the following error:

<interactive>:1:1: error:
    Not in scope: ‘M.assocs’
    No module named ‘M’ is imported.

Upvotes: 0

Views: 680

Answers (2)

I figured out the answer. In Data.HashMap.Lazy the method toList performs the same function as assocs. As such the following code works.

import qualified Data.HashMap.Lazy as M
test = M.fromList [("a", 1), ("b", 2)]
M.toList test

Upvotes: 1

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

Data.HashMap.Lazy, from unordered-containers, does not export an assocs function.

You might be thinking of Data.HashMap from the hashmap package.

Upvotes: 2

Related Questions