module Data.StrMap where
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text (Text)
import qualified Data.Text as Text
newtype StrMap k a = StrMap (Map (AsStr k) a)
deriving (Eq, Ord, Show)
instance Semigroup a => Semigroup (StrMap k a) where
StrMap as <> StrMap bs = StrMap $
Map.unionWith (<>) as bs
instance Semigroup a => Monoid (StrMap k a) where
mempty = StrMap mempty
insert :: Show k => k -> a -> StrMap k a -> StrMap k a
insert k a (StrMap m) = StrMap (Map.insert (asStr k) a m)
lookup :: Show k => k -> StrMap k a -> Maybe a
lookup k (StrMap m) = Map.lookup (asStr k) m
data AsStr k = AsStr { asStrText :: Text, asStrValue :: k } deriving Show
instance Eq (AsStr k) where
AsStr a _ == AsStr b _ = a == b
instance Ord (AsStr k) where
AsStr a _ `compare` AsStr b _ = compare a b
asStr :: Show k => k -> AsStr k
asStr k = AsStr (Text.pack (show k)) k