dp wiz avatar dp wiz committed 421ac42

Data.Map conversion

Comments (0)

Files changed (1)

src/Data/EDN/Types/Class.hs

 import qualified Data.ByteString.Lazy.Char8 as BSL
 import qualified Data.Vector as V
 import qualified Data.Set as S
+import qualified Data.Map as M
 
 import Data.Parser as P
 import qualified Data.EDN.Types as E
     parseEDNv v = typeMismatch "Set" v
     {-# INLINE parseEDNv #-}
 
+instance (ToEDN a, ToEDN b) => ToEDN (M.Map a b) where
+    toEDNv m = E.Map $! M.fromList [(toEDNv k, toEDN v) | (k, v) <- M.assocs m]
+    {-# INLINE toEDNv #-}
+
+instance (Ord a, FromEDN a, FromEDN b) => FromEDN (M.Map a b) where
+    parseEDNv (E.Map m) = mapMmap parseEDNv parseEDN m
+    parseEDNv v = typeMismatch "Map" v
+    {-# INLINE parseEDNv #-}
+
 instance ToEDN E.Value where
     toEDNv = id
 
 mapMset :: (Monad m, Ord b) => (a -> m b) -> S.Set a -> m (S.Set b)
 mapMset f s = mapM f (S.toList s) >>= return . S.fromList
 {-# INLINE mapMset #-}
+
+mapMmap :: (Ord a2, Monad m) => (a1 -> m a2) -> (b1 -> m b2) -> M.Map a1 b1 -> m (M.Map a2 b2)
+mapMmap kf vf m = do
+    let pairsIn = M.assocs m
+    pairsOut <- mapM fs pairsIn
+    return $! M.fromList pairsOut
+    where
+        fs (k, v) = do
+            newK <- kf k
+            newV <- vf v
+            return (newK, newV)
+{-# INLINE mapMmap #-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.