nonemptymap 0.0.4.0 → 0.0.6.0
raw patch · 2 files changed
+67/−11 lines, 2 filesdep ~containersPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: containers
API changes (from Hackage documentation)
+ Data.Map.NonEmpty: fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Maybe (NonEmptyMap k a)
+ Data.Map.NonEmpty: fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Maybe (NonEmptyMap k a)
+ Data.Map.NonEmpty: fromNonEmptyWith :: Ord k => (t -> t -> t) -> NonEmpty (k, t) -> NonEmptyMap k t
+ Data.Map.NonEmpty: fromNonEmptyWithKey :: Ord k => (k -> a -> a -> a) -> NonEmpty (k, a) -> NonEmptyMap k a
+ Data.Map.NonEmpty: map :: (t -> b) -> NonEmptyMap k t -> NonEmptyMap k b
+ Data.Map.NonEmpty: mapKeys :: Ord k => (t2 -> k) -> NonEmptyMap t2 t1 -> NonEmptyMap k t1
+ Data.Map.NonEmpty: mapKeysWith :: Ord k => (t1 -> t1 -> t1) -> (t2 -> k) -> NonEmptyMap t2 t1 -> NonEmptyMap k t1
+ Data.Map.NonEmpty: mapWithKey :: (t -> b) -> NonEmptyMap k t -> NonEmptyMap k b
- Data.Map.NonEmpty: NonEmptyMap :: (k, a) -> (Map k a) -> NonEmptyMap k a
+ Data.Map.NonEmpty: NonEmptyMap :: (k, a) -> Map k a -> NonEmptyMap k a
Files
- nonemptymap.cabal +2/−2
- src/Data/Map/NonEmpty.hs +65/−9
nonemptymap.cabal view
@@ -1,5 +1,5 @@ name: nonemptymap-version: 0.0.4.0+version: 0.0.6.0 synopsis: A NonEmptyMap Implementation description: This package intends to allow general use of a NonEmptyMap which is very beneficial as sometimes you want the functionality@@ -19,7 +19,7 @@ hs-source-dirs: src exposed-modules: Data.Map.NonEmpty build-depends: base >= 4.7 && < 5- , containers >= 0.5.8 && < 0.6+ , containers >= 0.5.8 && < 0.7 , semigroupoids >= 5 && < 6 default-language: Haskell2010
src/Data/Map/NonEmpty.hs view
@@ -24,7 +24,11 @@ -- * Construction , singleton -- :: (k, a) -> NonEmptyMap k v , fromList -- :: Ord k => [(k, a)] -> Maybe (NonEmptyMap k a)+ , fromListWith -- :: Ord k => (a -> a -> a) -> [(k, a)] -> Maybe (NonEmptyMap k a)+ , fromListWithKey -- :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Maybe (NonEmptyMap k a) , fromNonEmpty -- :: Ord k => NonEmpty (k, a) -> NonEmptyMap k a+ , fromNonEmptyWith -- :: Ord k => (t -> t -> t) -> NonEmpty (k, t) -> NonEmptyMap k t+ , fromNonEmptyWithKey -- :: Ord k => (k -> a -> a -> a) -> NonEmpty (k, a) -> NonEmptyMap k a -- * Insertion , insert -- :: Ord k => k -> a -> NonEmptyMap k a -> NonEmptyMap k a , insertWith -- :: Ord k => (a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a@@ -32,7 +36,7 @@ , insertLookupWithKey -- :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> (Maybe a, NonEmptyMap k a) -- * Deletion/Update , delete -- :: Ord k => k -> NonEmptyMap k a -> Map.Map k a- , adjust -- :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a + , adjust -- :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a , update -- :: Ord k => (a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a , alter -- :: Ord k => (Maybe a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a , alterF -- :: forall f k a. (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> NonEmptyMap k a -> f (Map.Map k a)@@ -42,12 +46,17 @@ , findWithDefault -- :: Ord k => a -> k -> NonEmptyMap k a -> a , member -- :: Ord k => k -> NonEmptyMap k a -> Bool , notMember -- :: Ord k => k -> NonEmptyMap k a -> Bool- -- * Size + -- * Size , size -- :: NonEmptyMap k a -> In -- * Conversions , toList -- :: NonEmptyMap k a -> [(k, a)] , Data.Map.NonEmpty.toNonEmpty -- :: NonEmptyMap k a -> NonEmpty (k, a) , toMap -- :: Ord k => NonEmptyMap k a -> Map.Map k a+ -- * Map+ , map -- :: (t -> b) -> NonEmptyMap k t -> NonEmptyMap k b+ , mapWithKey -- :: (t -> b) -> NonEmptyMap k t -> NonEmptyMap k b+ , mapKeys -- :: Ord k => (t2 -> k) -> NonEmptyMap t2 t1 -> NonEmptyMap k t1+ , mapKeysWith -- :: Ord k => (t1 -> t1 -> t1) -> (t2 -> k) -> NonEmptyMap t2 t1 -> NonEmptyMap k t1 ) where import qualified Data.Map as Map@@ -60,8 +69,9 @@ import Data.Semigroup.Foldable (Foldable1(..)) import Data.List.NonEmpty (NonEmpty(..)) import qualified Data.List.NonEmpty as NonEmptyList+import qualified Data.List as List -import Prelude hiding (lookup)+import Prelude hiding (lookup, map) -- | A NonEmptyMap of keys k to values a@@ -125,7 +135,7 @@ instance Foldable1 (NonEmptyMap k) where foldMap1 :: Semigroup m => (a -> m) -> NonEmptyMap k a -> m foldMap1 f (NonEmptyMap (k, a) m) = Map.foldr ((<>) . f) (f a) m- + -- Construction singleton :: (k, a) -> NonEmptyMap k a singleton tup = NonEmptyMap tup Map.empty@@ -137,6 +147,23 @@ fromNonEmpty :: Ord k => NonEmpty (k, a) -> NonEmptyMap k a fromNonEmpty nel = NonEmptyMap (NonEmptyList.head nel) (Map.fromList (NonEmptyList.tail nel)) +fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Maybe (NonEmptyMap k a)+fromListWithKey _ [] = Nothing+fromListWithKey f (x:xs) = Just $ foldlStrict ins (NonEmptyMap (fst x, snd x) Map.empty) xs+ where+ ins t (k, v) = insertWithKey f k v t++fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Maybe (NonEmptyMap k a)+fromListWith f xs = fromListWithKey (\_ x y -> f x y) xs++fromNonEmptyWithKey :: Ord k => (k -> a -> a -> a) -> NonEmpty (k, a) -> NonEmptyMap k a+fromNonEmptyWithKey f (x :| xs) = foldlStrict ins (NonEmptyMap x Map.empty) xs+ where+ ins t (k, v) = insertWithKey f k v t++fromNonEmptyWith :: Ord k => (t -> t -> t) -> NonEmpty (k, t) -> NonEmptyMap k t+fromNonEmptyWith f xs = fromNonEmptyWithKey (\_ x y -> f x y) xs+ {-------------------------------------------------------------------- Insertion --------------------------------------------------------------------}@@ -149,12 +176,12 @@ insertWith f key value (NonEmptyMap (k, a) m) = NonEmptyMap (k, a) (Map.insertWith f key value m) insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a-insertWithKey f key value (NonEmptyMap (k, a) m) = +insertWithKey f key value (NonEmptyMap (k, a) m) = if k == key then NonEmptyMap (key, f key value a) m else NonEmptyMap (k, a) (Map.insertWithKey f key value m) insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> (Maybe a, NonEmptyMap k a)-insertLookupWithKey f key value (NonEmptyMap (k, a) m) = +insertLookupWithKey f key value (NonEmptyMap (k, a) m) = if k == key then (Just a, NonEmptyMap(key, f key value a) m) else fmap (NonEmptyMap (k, a)) (Map.insertLookupWithKey f key value m) @@ -165,7 +192,7 @@ delete key (NonEmptyMap (k, a) m) | key == k = m delete key (NonEmptyMap (k, a) m) = Map.insert k a (Map.delete k m) -adjust :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a +adjust :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a adjust f key (NonEmptyMap (k, a) m) | key == k = NonEmptyMap (key, f a) m adjust f key (NonEmptyMap (k, a) m) = NonEmptyMap (k, a) (Map.adjust f key m) @@ -181,7 +208,7 @@ Nothing -> m alter f key (NonEmptyMap (k, a) m) = Map.insert k a (Map.alter f key m) -alterF :: forall f k a. (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> NonEmptyMap k a -> f (Map.Map k a) +alterF :: forall f k a. (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> NonEmptyMap k a -> f (Map.Map k a) alterF f key (NonEmptyMap (k, a) m) | key == k = insideF <$> f (Just a) where insideF :: Maybe a -> Map.Map k a@@ -213,7 +240,7 @@ Size --------------------------------------------------------------------} size :: NonEmptyMap k a -> Int-size (NonEmptyMap _ m) = 1 + Map.size m +size (NonEmptyMap _ m) = 1 + Map.size m {-------------------------------------------------------------------- Conversions@@ -228,3 +255,32 @@ toMap :: Ord k => NonEmptyMap k a -> Map.Map k a toMap (NonEmptyMap (k, a) m) = Map.insert k a m+++{--------------------------------------------------------------------+ Map+--------------------------------------------------------------------}++mapWithKey :: (t -> b) -> NonEmptyMap k t -> NonEmptyMap k b+mapWithKey f (NonEmptyMap (k, v) map) = NonEmptyMap (k, f v) (Map.map f map)++map :: (t -> b) -> NonEmptyMap k t -> NonEmptyMap k b+map = mapWithKey++mapKeysWith :: Ord k => (t1 -> t1 -> t1) -> (t2 -> k) -> NonEmptyMap t2 t1 -> NonEmptyMap k t1+mapKeysWith c f = fromNonEmptyWith c . NonEmptyList.map fFirst . Data.Map.NonEmpty.toNonEmpty+ where+ fFirst (x, y) = (f x, y)++mapKeys :: Ord k => (t2 -> k) -> NonEmptyMap t2 t1 -> NonEmptyMap k t1+mapKeys = mapKeysWith (\x _ -> x)+++{--------------------------------------------------------------------+ Utils+--------------------------------------------------------------------}++foldlStrict :: (a -> b -> a) -> a -> [b] -> a+foldlStrict f z xs = case xs of+ [] -> z+ (x:xss) -> let z' = f z x in seq z' (foldlStrict f z' xss)