From c72a0a292d3b3e5b0c6d0c5cf395e658556eb4a2 Mon Sep 17 00:00:00 2001 From: joe Date: Wed, 13 Aug 2014 18:35:18 -0400 Subject: more IMap k a --- IntMapClass.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'IntMapClass.hs') diff --git a/IntMapClass.hs b/IntMapClass.hs index ce544f9..77ce16b 100644 --- a/IntMapClass.hs +++ b/IntMapClass.hs @@ -89,6 +89,7 @@ lookup = adapt_k_m (IntMap.lookup) findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x findWithDefault a = adapt_k_m (IntMap.findWithDefault a) +-- FIXME: fmap (first coerce) probably incurs cost lookupLT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) lookupLT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLT) k m lookupGT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) @@ -194,6 +195,7 @@ map f = IMap . adapt_m (IntMap.map f) mapWithKey :: Coercible Int k => (k -> a -> b) -> IMap k a -> IMap k b mapWithKey f = IMap . adapt_m (IntMap.mapWithKey $ f . coerce) +-- FIXME: fmap IMap ? traverseWithKey :: (Applicative f, Coercible Int k) => (k -> a -> f b) -> IMap k a -> f (IMap k b) @@ -283,4 +285,45 @@ fromListWithKey :: ( Coercible Int k (k -> a -> a -> a) -> [(k, a)] -> IMap k a fromListWithKey f = IMap . IntMap.fromListWithKey (f . coerce) . coerce +toAscList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k,a)] +toAscList (IMap m) = coerce $ IntMap.toAscList m + +toDescList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k,a)] +toDescList (IMap m) = coerce $ IntMap.toDescList m + +fromAscList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a +fromAscList = IMap . IntMap.fromAscList . coerce + +fromAscListWith :: + Coercible [(k,a)] [(Int, a)] => + (a -> a -> a) -> [(k,a)] -> IMap k a +fromAscListWith f = IMap . IntMap.fromAscListWith f . coerce + +fromAscListWithKey :: + (Coercible Int k, Coercible [(k,a)] [(Int, a)]) => + (k -> a -> a -> a) -> [(k,a)] -> IMap k a +fromAscListWithKey f = IMap . IntMap.fromAscListWithKey (f . coerce) . coerce + +fromDistinctAscList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a +fromDistinctAscList = IMap . IntMap.fromDistinctAscList . coerce + +filter :: (a -> Bool) -> IMap k a -> IMap k a +filter f = IMap . adapt_m (IntMap.filter f) + +filterWithKey :: Coercible Int k => (k -> a -> Bool) -> IMap k a -> IMap k a +filterWithKey f = IMap . adapt_m (IntMap.filterWithKey $ f . coerce) + +partition :: Coercible (IntMap a,IntMap a) (IMap k a,IMap k a) + => (a -> Bool) -> IMap k a -> (IMap k a, IMap k a) +partition f m = coerce $ IntMap.partition f (intmap m) + + +partitionWithKey :: ( Coercible Int k + , Coercible (IntMap a,IntMap a) (IMap k a,IMap k a) ) + => (k -> a -> Bool) -> IMap k a -> (IMap k a, IMap k a) +partitionWithKey f m = coerce $ IntMap.partitionWithKey (f . coerce) (intmap m) + +mapMaybe :: (a -> Maybe b) -> IMap k a -> IMap k b +mapMaybe f = IMap . IntMap.mapMaybe f . intmap + -- cgit v1.2.3