diff options
author | joe <joe@jerkface.net> | 2014-08-12 16:40:51 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-08-12 16:40:51 -0400 |
commit | b1a9f8026ec205fbce84680a7e2a7847c49c0098 (patch) | |
tree | b3247250e040bb8b3c51a23637710fa47c75185b | |
parent | ebfbdcd09b92a55b6c9f5a1f7feaa4f98c2236ba (diff) |
IntMapClass.hs updates
-rw-r--r-- | IntMapClass.hs | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/IntMapClass.hs b/IntMapClass.hs index 49e1125..9ab83db 100644 --- a/IntMapClass.hs +++ b/IntMapClass.hs | |||
@@ -2,6 +2,7 @@ | |||
2 | FlexibleContexts, | 2 | FlexibleContexts, |
3 | MultiParamTypeClasses, | 3 | MultiParamTypeClasses, |
4 | GeneralizedNewtypeDeriving, | 4 | GeneralizedNewtypeDeriving, |
5 | DeriveTraversable, | ||
5 | DeriveDataTypeable #-} | 6 | DeriveDataTypeable #-} |
6 | module IntMapClass where | 7 | module IntMapClass where |
7 | 8 | ||
@@ -19,7 +20,7 @@ import Data.Coerce | |||
19 | class Coercible a b where coerce :: a -> b | 20 | class Coercible a b where coerce :: a -> b |
20 | #endif | 21 | #endif |
21 | 22 | ||
22 | newtype IMap k a = IMap (IntMap a) | 23 | newtype IMap k a = IMap { intmap :: IntMap a } |
23 | deriving | 24 | deriving |
24 | ( Functor | 25 | ( Functor |
25 | , Typeable | 26 | , Typeable |
@@ -37,5 +38,41 @@ newtype IMap k a = IMap (IntMap a) | |||
37 | adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x | 38 | adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x |
38 | adapt_m_k f (IMap m) k = f m (coerce k) | 39 | adapt_m_k f (IMap m) k = f m (coerce k) |
39 | 40 | ||
41 | adapt_k_m :: Coercible k Int => (Int -> IntMap a -> x) -> k -> IMap k a -> x | ||
42 | adapt_k_m f k (IMap m) = f (coerce k) m | ||
43 | -- adapt_k_m2 :: Coercible k Int => (Int -> IntMap a -> x) -> k -> IMap k a -> x | ||
44 | -- adapt_k_m2 f k m = (adapt_k f) k (intmap m) | ||
45 | -- adapt_k :: Coercible k Int => (Int -> x) -> k -> x | ||
46 | -- adapt_k f k = f (coerce k) | ||
47 | |||
48 | adapt_m_m :: (IntMap a -> IntMap a -> x) -> IMap k a -> IMap k a -> x | ||
49 | adapt_m_m f m = adapt_m (adapt_m f m) | ||
50 | |||
51 | adapt_m :: (IntMap a -> x) -> IMap k a -> x | ||
52 | adapt_m f (IMap m) = f m | ||
53 | |||
54 | |||
40 | (!) :: Coercible k Int => IMap k a -> k -> a | 55 | (!) :: Coercible k Int => IMap k a -> k -> a |
41 | (!) = adapt_m_k (IntMap.!) | 56 | (!) = adapt_m_k (IntMap.!) |
57 | |||
58 | (\\) :: IMap k a -> IMap k a -> IMap k a | ||
59 | (\\) a b = IMap $ adapt_m_m (IntMap.\\) a b | ||
60 | |||
61 | null = adapt_m (IntMap.null) | ||
62 | size = adapt_m (IntMap.size) | ||
63 | |||
64 | member :: Coercible k Int => k -> IMap k a -> Bool | ||
65 | member = adapt_k_m (IntMap.member) | ||
66 | |||
67 | notMember :: Coercible k Int => k -> IMap k a -> Bool | ||
68 | notMember = adapt_k_m (IntMap.notMember) | ||
69 | |||
70 | lookup :: Coercible k Int => k -> IMap k a -> Maybe a | ||
71 | lookup = adapt_k_m (IntMap.lookup) | ||
72 | |||
73 | findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x | ||
74 | findWithDefault a = adapt_k_m (IntMap.findWithDefault a) | ||
75 | |||
76 | lookupLT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | ||
77 | lookupLT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLT) k m | ||
78 | where first f (x,y) = (f x,y) | ||