Useful-0.0.2: Some useful functions and shorthands.




A lightweight Dictionary implementation based on Data.Map, part of the Useful module.

So I kinda like dictionaries, but the functions and syntax by default are hardly as elegant as something like python. This isn't a complete solution and nor is it optimal but it's pretty lightweight and pretty and stuff. You get it. It's based off Data.Map so uses a binary tree and therefore the keys have to have some ordering defined over them.


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 => k -> Map k a -> Maybe aSource

Returns Maybe v from key k

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

Returns v from key k or error.

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

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

checks for a key in a dictionary.

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

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

Deletes ALL key-pairs from a dictionary given they match a value

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

Intersects two dictionaries

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

Unions two dictionaries

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

Tests if d1 is a sub-dictionary of d2

(#?!) :: (Eq a, Ord k) => Map k a -> a -> kSource

Returns a the first occurance of a key from a value. Otherwise error.

dictSize :: Map k a -> IntSource

Returns the size of a dictionary

mapD :: (a -> b) -> Map k a -> Map k bSource

Data.Maps a function to all values in a dictionary

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

Data.Maps a function to all keys in a dictionary

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

filter on a dincationy, you get the idea

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

filter for keys