{-# LANGUAGE Safe #-}
module Data.Map.Utils (
flipM, flippedLookupM, forceLookupM,
strToM,
strFromM
)
where
import Data.List.Utils (flipAL, strFromAL, strToAL)
import qualified Data.Map
strFromM :: (Show a, Show b, Ord a) => Data.Map.Map a b -> String
strFromM :: forall a b. (Show a, Show b, Ord a) => Map a b -> String
strFromM = forall a b. (Show a, Show b) => [(a, b)] -> String
strFromAL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
Data.Map.toList
strToM :: (Read a, Read b, Ord a) => String -> Data.Map.Map a b
strToM :: forall a b. (Read a, Read b, Ord a) => String -> Map a b
strToM = forall k a. Ord k => [(k, a)] -> Map k a
Data.Map.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Read a, Read b) => String -> [(a, b)]
strToAL
flipM :: (Ord key, Ord val) => Data.Map.Map key val -> Data.Map.Map val [key]
flipM :: forall key val. (Ord key, Ord val) => Map key val -> Map val [key]
flipM = forall k a. Ord k => [(k, a)] -> Map k a
Data.Map.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall key val. (Eq key, Eq val) => [(key, val)] -> [(val, [key])]
flipAL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
Data.Map.toList
flippedLookupM :: (Ord val, Ord key) => val -> Data.Map.Map key val -> [key]
flippedLookupM :: forall val key. (Ord val, Ord key) => val -> Map key val -> [key]
flippedLookupM val
v Map key val
fm =
case forall k a. Ord k => k -> Map k a -> Maybe a
Data.Map.lookup val
v (forall key val. (Ord key, Ord val) => Map key val -> Map val [key]
flipM Map key val
fm) of
Maybe [key]
Nothing -> []
Just [key]
x -> [key]
x
forceLookupM :: (Show key, Ord key) => String -> key ->
Data.Map.Map key elt -> elt
forceLookupM :: forall key elt.
(Show key, Ord key) =>
String -> key -> Map key elt -> elt
forceLookupM String
msg key
k Map key elt
fm =
case forall k a. Ord k => k -> Map k a -> Maybe a
Data.Map.lookup key
k Map key elt
fm of
Just elt
x -> elt
x
Maybe elt
Nothing -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
msg forall a. [a] -> [a] -> [a]
++ String
": could not find key " forall a. [a] -> [a] -> [a]
++ (forall a. Show a => a -> String
show key
k)