summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-08-12 16:40:51 -0400
committerjoe <joe@jerkface.net>2014-08-12 16:40:51 -0400
commitb1a9f8026ec205fbce84680a7e2a7847c49c0098 (patch)
treeb3247250e040bb8b3c51a23637710fa47c75185b
parentebfbdcd09b92a55b6c9f5a1f7feaa4f98c2236ba (diff)
IntMapClass.hs updates
-rw-r--r--IntMapClass.hs39
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 #-}
6module IntMapClass where 7module IntMapClass where
7 8
@@ -19,7 +20,7 @@ import Data.Coerce
19class Coercible a b where coerce :: a -> b 20class Coercible a b where coerce :: a -> b
20#endif 21#endif
21 22
22newtype IMap k a = IMap (IntMap a) 23newtype 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)
37adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x 38adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x
38adapt_m_k f (IMap m) k = f m (coerce k) 39adapt_m_k f (IMap m) k = f m (coerce k)
39 40
41adapt_k_m :: Coercible k Int => (Int -> IntMap a -> x) -> k -> IMap k a -> x
42adapt_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
48adapt_m_m :: (IntMap a -> IntMap a -> x) -> IMap k a -> IMap k a -> x
49adapt_m_m f m = adapt_m (adapt_m f m)
50
51adapt_m :: (IntMap a -> x) -> IMap k a -> x
52adapt_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
61null = adapt_m (IntMap.null)
62size = adapt_m (IntMap.size)
63
64member :: Coercible k Int => k -> IMap k a -> Bool
65member = adapt_k_m (IntMap.member)
66
67notMember :: Coercible k Int => k -> IMap k a -> Bool
68notMember = adapt_k_m (IntMap.notMember)
69
70lookup :: Coercible k Int => k -> IMap k a -> Maybe a
71lookup = adapt_k_m (IntMap.lookup)
72
73findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x
74findWithDefault a = adapt_k_m (IntMap.findWithDefault a)
75
76lookupLT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a)
77lookupLT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLT) k m
78 where first f (x,y) = (f x,y)