Useful-0.0.5: Some useful functions and shorthands.




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.


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

dictSize :: Map k a -> IntSource

Returns the size of a dictionary [Data.Map.size]

Dictionary operations

(#!) :: Ord k => k -> Map k a -> Maybe aSource

Returns Maybe v from key k [Data.Map.lookup]

(#!!) :: Ord k => Map k a -> k -> aSource

Returns v from key k or error [Data.Map.!]

(#+) :: 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 -> BoolSource

Checks for a key in a dictionary. [Data.Map.member]

(#*?) :: (Eq a, Ord k) => Map k a -> a -> BoolSource

Checks if a value is in a dictionary

(#-) :: 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 => Map k a -> Map k b -> Map k aSource

Intersects two dictionaries [Data.Map.\]

(#++) :: Ord k => Map k a -> Map k a -> Map k aSource

Unions two dictionaries [Data.Map.union]

(#??) :: (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 []

mapDkeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 aSource

Maps a function to all keys in a dictionary [Data.Map.mapKeys]

filterD :: Ord k => (a -> Bool) -> Map k a -> Map k aSource

Filter over dictionary values [Data.Map.filter]

filterDkeys :: Ord k => (k -> a -> Bool) -> Map k a -> Map k aSource

Filter over keys in a dictionary [Data.Map.filterWithKey]