From 78a6247d65684cb9c0690b7f9eb6e0ceb0d433d9 Mon Sep 17 00:00:00 2001 From: joe Date: Wed, 13 Aug 2014 16:57:50 -0400 Subject: more IntMapClass --- IntMapClass.hs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'IntMapClass.hs') diff --git a/IntMapClass.hs b/IntMapClass.hs index 7c18de8..ce544f9 100644 --- a/IntMapClass.hs +++ b/IntMapClass.hs @@ -201,3 +201,86 @@ traverseWithKey f = fmap IMap . adapt_m (IntMap.traverseWithKey $ f . coerce) mapAccum :: (t -> b -> (t, a)) -> t -> IMap k b -> (t, IMap k a) mapAccum f a m = second IMap $ IntMap.mapAccum f a (intmap m) + +mapAccumWithKey :: Coercible Int k => + (t -> k -> b -> (t, a)) -> t -> IMap k b -> (t, IMap k a) +mapAccumWithKey f a m = second IMap $ IntMap.mapAccumWithKey f' a (intmap m) + where f' a k b = f a (coerce k) b + + +mapAccumRWithKey :: Coercible Int k => + (t -> k -> b -> (t, a)) -> t -> IMap k b -> (t, IMap k a) +mapAccumRWithKey f a m = second IMap $ IntMap.mapAccumRWithKey f' a (intmap m) + where f' a k b = f a (coerce k) b + +mapKeys :: (Coercible Int k1, Coercible k2 Int) => + (k1 -> k2) -> IMap k1 a -> IMap k2 a +mapKeys f = IMap . adapt_m (IntMap.mapKeys (coerce . f . coerce)) + +mapKeysWith :: (Coercible Int k1, Coercible k2 Int) => + (a->a->a) -> (k1 -> k2) -> IMap k1 a -> IMap k2 a +mapKeysWith c f = IMap . adapt_m (IntMap.mapKeysWith c (coerce . f . coerce)) + +mapKeysMonotonic :: (Coercible Int k1, Coercible k2 Int) => + (k1 -> k2) -> IMap k1 a -> IMap k2 a +mapKeysMonotonic f = IMap . adapt_m (IntMap.mapKeysMonotonic (coerce . f . coerce)) + +foldr :: (a -> x -> x) -> x -> IMap k a -> x +foldr f b = adapt_m (IntMap.foldr f b) + +foldl :: (x -> a -> x) -> x -> IMap k a -> x +foldl f a = adapt_m (IntMap.foldl f a) + +foldrWithKey :: Coercible Int b => (b -> a -> x -> x) -> x -> IMap k a -> x +foldrWithKey f b = adapt_m (IntMap.foldrWithKey (f . coerce) b) + +foldlWithKey :: + Coercible Int k => (x -> k -> a -> x) -> x -> IMap k a -> x +foldlWithKey f a = adapt_m (IntMap.foldlWithKey f' a) where f' a = f a . coerce + +#if MIN_VERSION_containers(0,5,3) +foldMapWithKey :: (Monoid m, Coercible k Int) => (k -> a -> m) -> IMap k a -> m +foldMapWithKey f = adapt_m (IntMap.foldMapWithKey $ f . coerce) +#endif + +foldr' :: (a -> x -> x) -> x -> IMap k a -> x +foldr' f b = adapt_m (IntMap.foldr' f b) + +foldl' :: (a -> x -> a) -> a -> IMap k x -> a +foldl' f b = adapt_m (IntMap.foldl' f b) + +foldrWithKey' :: Coercible Int b => (b -> a -> x -> x) -> x -> IMap k a -> x +foldrWithKey' f b = adapt_m (IntMap.foldrWithKey' (f . coerce) b) + +foldlWithKey' :: + Coercible Int k => (x -> k -> a -> x) -> x -> IMap k a -> x +foldlWithKey' f a = adapt_m (IntMap.foldlWithKey' f' a) where f' a = f a . coerce + +elems :: IMap k a -> [a] +elems = IntMap.elems . intmap + +keys :: Coercible [Int] [k] => IMap k a -> [k] +keys = coerce . IntMap.keys . intmap + +assocs :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k, a)] +assocs = coerce . IntMap.assocs . intmap + +-- Not implementing... (doing it right requires wrapping IntSet) +-- keysSet :: IntMap a -> IntSet +-- fromSet :: (Key -> a) -> IntSet -> IntMap a + +toList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k, a)] +toList = coerce . IntMap.toList . intmap + +fromList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a +fromList = IMap . IntMap.fromList . coerce + +fromListWith :: Coercible [(k,a)] [(Int,a)] => (a -> a -> a) -> [(k, a)] -> IMap k a +fromListWith f = IMap . IntMap.fromListWith f . coerce + +fromListWithKey :: ( Coercible Int k + , Coercible [(k,a)] [(Int,a)] ) => + (k -> a -> a -> a) -> [(k, a)] -> IMap k a +fromListWithKey f = IMap . IntMap.fromListWithKey (f . coerce) . coerce + + -- cgit v1.2.3