A lightweight Dictionary implementation based on Data.Map, part of the Useful module.
I like dictionaries, and use them often for small things when I'm coding, but the functions and syntax by default are hardly as elegant as something like python. This is one thing I feel is missing in the default implementation of data types. Also, the clashing namespace when importing Data.Map makes code often hard to read and long. Completely inconvenient for small, simple tasks. This isn't a complete solution and nor is it optimal but it's lightweight and pretty. Keys must have some ordering defined over them. In the function descriptions, listed in square brackets are the Data.Map functions used - this does not mean it is an exact alias though, it make just use it.
- dict :: Ord k => [(k, a)] -> Map k a
- dictToList :: Map k a -> [(k, a)]
- dictSize :: Map k a -> Int
- (#!) :: Ord k => k -> Map k a -> Maybe a
- (#!!) :: Ord k => Map k a -> k -> a
- (#+) :: Ord k => Map k a -> (k, a) -> Map k a
- (#?) :: Ord k => Map k a -> k -> Bool
- (#*?) :: (Eq a, Ord k) => Map k a -> a -> Bool
- (#-) :: Ord k => Map k a -> k -> Map k a
- (#*-) :: (Eq a, Ord k) => Map k a -> a -> Map k a
- (#\\) :: Ord k => Map k a -> Map k b -> Map k a
- (#++) :: Ord k => Map k a -> Map k a -> Map k a
- (#??) :: (Ord k, Eq a) => Map k a -> Map k a -> Bool
- (#?!) :: (Eq a, Ord k) => Map k a -> a -> k
- mapD :: (a -> b) -> Map k a -> Map k b
- mapDkeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
- filterD :: Ord k => (a -> Bool) -> Map k a -> Map k a
- filterDkeys :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a
Dictionary creation
dict :: Ord k => [(k, a)] -> Map k aSource
Alias of Data.Map.fromList, takes a list of key value tuples and creates a dictionary out of them.
dict [("hello",1),("there",2)] fromList [("hello",1),("there",2)] dict [] fromList []
dictToList :: Map k a -> [(k, a)]Source
Returns a List of key-value pairs
Dictionary operations
(#+) :: Ord k => Map k a -> (k, a) -> Map k aSource
Adds key-value pair to a dictionary. If key is already in dict will update value. [Data.Map.insert]
(#-) :: Ord k => Map k a -> k -> Map k aSource
Deletes a key-pair from a dictionary given a key [Data.Map.delete]
(#*-) :: (Eq a, Ord k) => Map k a -> a -> Map k aSource
Deletes ALL key-pairs from a dictionary given they match a value [Data.Map.filter]
(#??) :: (Ord k, Eq a) => Map k a -> Map k a -> BoolSource
Tests if d1 is a sub-dictionary of d2 [Data.Map.isSubmapOf]
(#?!) :: (Eq a, Ord k) => Map k a -> a -> kSource
Returns a the first occurance of a key from a value. Otherwise error.
Dictionary mapping and filtering
mapD :: (a -> b) -> Map k a -> Map k bSource
Maps a function to all values in a dictionary [Data.Map.map]
mapDkeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 aSource
Maps a function to all keys in a dictionary [Data.Map.mapKeys]