git.iko.soy/typeable/refined-containers
on main help sign in
Add zipWith
commitffbe29f371456e62215465bcc05de741bf2712cd authormniip <mniip@mniip.com> dateWed 5 Mar 2025, 14:33 (2 years ago) parenta959064
8 files changed+85−34
src/Data/HashMap/Common/Refined.hs+6−6 src/Data/HashMap/Refined.hs+11−2 src/Data/HashMap/Strict/Refined.hs+3−2 src/Data/IntMap/Common/Refined.hs+10−9 src/Data/IntMap/Refined.hs+22−2 src/Data/IntMap/Strict/Refined.hs+3−2 src/Data/Map/Common/Refined.hs+10−9 src/Data/Map/Refined.hs+20−2
src/Data/HashMap/Common/Refined.hs+6−6
@@ -24,6 +24,7 @@ import Data.Reflection
2424import Data.Traversable.WithIndex
2525import Data.Type.Coercion
2626import Data.Type.Equality ((:~:)(..))
27+import Prelude hiding (zipWith)
2728import Refined
2829import Refined.Unsafe
2930import Unsafe.Coerce
@@ -239,11 +240,10 @@ disjoint (HashMap m1) (HashMap m2)
239240
240241-- | Given two maps proven to have the same keys, for each key apply the
241242-- function to the associated values, to obtain a new map with the same keys.
242−zipWithKey
243+zipWith
243244 :: forall s k a b c. Hashable k
244− => (Key s k -> a -> b -> c) -> HashMap s k a -> HashMap s k b -> HashMap s k c
245−zipWithKey f (HashMap m1) (HashMap m2) = HashMap
246− $ HashMap.intersectionWithKey (f . unsafeKey) m1 m2
245+ => (a -> b -> c) -> HashMap s k a -> HashMap s k b -> HashMap s k c
246+zipWith f (HashMap m1) (HashMap m2) = HashMap $ HashMap.intersectionWith f m1 m2
247247
248248-- | Return the union of two maps. For keys that exist in both maps, the value
249249-- is taken from the first map.
@@ -439,7 +439,7 @@ instance TraversableWithIndex (Key s k) (HashMap s k) where
439439-- '<*>'/'Control.Applicative.liftA2' without 'KnownSet' see 'zipWithKey'.
440440instance (Hashable k, KnownHashSet s k) => Applicative (HashMap s k) where
441441 pure x = fromSet \_ -> x
442− (<*>) = zipWithKey (const id)
442+ (<*>) = zipWith id
443443
444444-- | @'bind' m f@ is a map that for each key @k :: 'Key' s k@, contains the
445445-- value @f (m '!' k) '!' k@, similar to @'>>='@ for functions.
@@ -462,7 +462,7 @@ instance (Hashable k, KnownHashSet s k)
462462
463463-- | Append the values at the corresponding keys
464464instance (Hashable k, Semigroup a) => Semigroup (HashMap s k a) where
465− (<>) = zipWithKey (const (<>))
465+ (<>) = zipWith (<>)
466466
467467instance (Hashable k, KnownHashSet s k, Monoid a)
468468 => Monoid (HashMap s k a) where
src/Data/HashMap/Refined.hs+11−2
@@ -74,6 +74,7 @@ module Data.HashMap.Refined
7474 , Common.disjoint
7575 , DisjointProof(..)
7676 -- * Combine
77+ , zipWith
7778 , zipWithKey
7879 , bind
7980 , Common.union
@@ -128,7 +129,7 @@ import Data.Container.Refined.Unsafe
128129import Data.Functor
129130import Data.HashMap.Common.Refined
130131 ( HashMap(..), Key, unsafeCastKey, unsafeKey, SomeHashMapWith(..)
131− , Some2HashMapWith(..), fromSet, (!), zipWithKey, mapWithKey, traverseWithKey
132+ , Some2HashMapWith(..), fromSet, (!), zipWith, mapWithKey, traverseWithKey
132133 , bind
133134 )
134135import qualified Data.HashMap.Common.Refined as Common
@@ -136,7 +137,7 @@ import qualified Data.HashMap.Lazy as HashMap
136137import Data.Traversable
137138import Data.Traversable.WithIndex
138139import Data.Type.Coercion
139−import Prelude hiding (lookup, null)
140+import Prelude hiding (lookup, null, zipWith)
140141import Refined
141142import Refined.Unsafe
142143
@@ -335,6 +336,14 @@ updateLookupWithKey f k (HashMap m) =
335336 $ SupersetProof unsafeSubset
336337 )
337338
339+-- | Given two maps proven to have the same keys, for each key apply the
340+-- function to the associated values, to obtain a new map with the same keys.
341+zipWithKey
342+ :: forall s k a b c. Hashable k
343+ => (Key s k -> a -> b -> c) -> HashMap s k a -> HashMap s k b -> HashMap s k c
344+zipWithKey f (HashMap m1) (HashMap m2) = HashMap
345+ $ HashMap.intersectionWithKey (f . unsafeKey) m1 m2
346+
338347-- | Return the union of two maps, with a given combining function for keys that
339348-- exist in both maps simultaneously.
340349unionWith
src/Data/HashMap/Strict/Refined.hs+3−2
@@ -74,6 +74,7 @@ module Data.HashMap.Strict.Refined
7474 , Common.disjoint
7575 , DisjointProof(..)
7676 -- * Combine
77+ , zipWith
7778 , zipWithKey
7879 , bind
7980 , Common.union
@@ -129,7 +130,7 @@ import Data.Functor
129130import qualified Data.HashMap.Strict as HashMap
130131import Data.HashMap.Common.Refined
131132 ( HashMap(..), Key, unsafeCastKey, unsafeKey, SomeHashMapWith(..)
132− , Some2HashMapWith(..), (!)
133+ , Some2HashMapWith(..), (!), zipWith
133134 )
134135import qualified Data.HashMap.Common.Refined as Common
135136import qualified Data.HashSet as HashSet
@@ -138,7 +139,7 @@ import Data.Reflection
138139import Data.Traversable
139140import Data.Traversable.WithIndex
140141import Data.Type.Coercion
141−import Prelude hiding (lookup, null)
142+import Prelude hiding (lookup, null, zipWith)
142143import Refined
143144import Refined.Unsafe
144145
src/Data/IntMap/Common/Refined.hs+10−9
@@ -20,6 +20,7 @@ import Data.Reflection
2020import Data.Traversable.WithIndex
2121import Data.Type.Coercion
2222import Data.Type.Equality ((:~:)(..))
23+import Prelude hiding (zipWith)
2324import Refined
2425import Refined.Unsafe
2526import Unsafe.Coerce
@@ -249,19 +250,19 @@ disjoint (IntMap m1) (IntMap m2)
249250
250251-- | Given two maps proven to have the same keys, for each key apply the
251252-- function to the associated values, to obtain a new map with the same keys.
252−zipWithKey
253− :: forall s a b c. (Key s -> a -> b -> c)
253+zipWith
254+ :: forall s a b c. (a -> b -> c)
254255 -> IntMap s a
255256 -> IntMap s b
256257 -> IntMap s c
257−zipWithKey f (IntMap m1) (IntMap m2) = IntMap
258− $ IntMap.mergeWithKey (\k x y -> Just $ f (unsafeKey k) x y)
258+zipWith f (IntMap m1) (IntMap m2) = IntMap
259+ $ IntMap.mergeWithKey (\_ x y -> Just $ f x y)
259260 (\m -> if IntMap.null m
260261 then IntMap.empty
261− else error "zipWithKey: bug: Data.IntMap.Refined has been subverted")
262+ else error "zipWith: bug: Data.IntMap.Refined has been subverted")
262263 (\m -> if IntMap.null m
263264 then IntMap.empty
264− else error "zipWithKey: bug: Data.IntMap.Refined has been subverted")
265+ else error "zipWith: bug: Data.IntMap.Refined has been subverted")
265266 -- ^ Work around https://github.com/haskell/containers/issues/979
266267 m1
267268 m2
@@ -520,10 +521,10 @@ instance TraversableWithIndex (Key s) (IntMap s) where
520521 itraverse = traverseWithKey
521522
522523-- | Similar to the instance for functions -- zip corresponding keys. To use
523−-- '<*>'/'Control.Applicative.liftA2' without 'KnownIntSet' see 'zipWithKey'.
524+-- '<*>'/'Control.Applicative.liftA2' without 'KnownIntSet' see 'zipWith'.
524525instance KnownIntSet s => Applicative (IntMap s) where
525526 pure x = fromSet \_ -> x
526− (<*>) = zipWithKey (const id)
527+ (<*>) = zipWith id
527528
528529-- | @'bind' m f@ is a map that for each key @k :: 'Key' s@, contains the
529530-- value @f (m '!' k) '!' k@, similar to @'>>='@ for functions.
@@ -543,7 +544,7 @@ instance KnownIntSet s => MonadReader (Key s) (IntMap s) where
543544
544545-- | Append the values at the corresponding keys
545546instance Semigroup a => Semigroup (IntMap s a) where
546− (<>) = zipWithKey (const (<>))
547+ (<>) = zipWith (<>)
547548
548549instance (KnownIntSet s, Monoid a) => Monoid (IntMap s a) where
549550 mempty = fromSet \_ -> mempty
src/Data/IntMap/Refined.hs+22−2
@@ -69,6 +69,7 @@ module Data.IntMap.Refined
6969 , Common.disjoint
7070 , DisjointProof(..)
7171 -- * Combine
72+ , zipWith
7273 , zipWithKey
7374 , bind
7475 , Common.union
@@ -135,13 +136,13 @@ import Data.Functor
135136import qualified Data.IntMap as IntMap
136137import Data.IntMap.Common.Refined
137138 ( IntMap(..), Key, unsafeCastKey, unsafeKey, SomeIntMapWith(..)
138− , Some2IntMapWith(..), fromSet, (!), zipWithKey, mapWithKey, traverseWithKey
139+ , Some2IntMapWith(..), fromSet, (!), zipWith, mapWithKey, traverseWithKey
139140 , bind
140141 )
141142import qualified Data.IntMap.Common.Refined as Common
142143import Data.Traversable
143144import Data.Type.Coercion
144−import Prelude hiding (lookup, null)
145+import Prelude hiding (lookup, null, zipWith)
145146import Refined
146147import Refined.Unsafe
147148
@@ -326,6 +327,25 @@ updateLookupWithKey f k (IntMap m)
326327 (v', !m') -> ((unsafeKey k,) <$> v',)
327328 $ SomeIntMapWith (IntMap m') $ SupersetProof unsafeSubset
328329
330+-- | Given two maps proven to have the same keys, for each key apply the
331+-- function to the associated values, to obtain a new map with the same keys.
332+zipWithKey
333+ :: forall s a b c. (Key s -> a -> b -> c)
334+ -> IntMap s a
335+ -> IntMap s b
336+ -> IntMap s c
337+zipWithKey f (IntMap m1) (IntMap m2) = IntMap
338+ $ IntMap.mergeWithKey (\k x y -> Just $ f (unsafeKey k) x y)
339+ (\m -> if IntMap.null m
340+ then IntMap.empty
341+ else error "zipWithKey: bug: Data.IntMap.Refined has been subverted")
342+ (\m -> if IntMap.null m
343+ then IntMap.empty
344+ else error "zipWithKey: bug: Data.IntMap.Refined has been subverted")
345+ -- ^ Work around https://github.com/haskell/containers/issues/979
346+ m1
347+ m2
348+
329349-- | Return the union of two maps, with a given combining function for keys that
330350-- exist in both maps simultaneously.
331351unionWith
src/Data/IntMap/Strict/Refined.hs+3−2
@@ -69,6 +69,7 @@ module Data.IntMap.Strict.Refined
6969 , Common.disjoint
7070 , DisjointProof(..)
7171 -- * Combine
72+ , zipWith
7273 , zipWithKey
7374 , bind
7475 , Common.union
@@ -135,14 +136,14 @@ import Data.Functor
135136import qualified Data.IntMap.Strict as IntMap
136137import Data.IntMap.Common.Refined
137138 ( IntMap(..), Key, unsafeCastKey, unsafeKey, SomeIntMapWith(..)
138− , Some2IntMapWith(..), (!)
139+ , Some2IntMapWith(..), (!), zipWith
139140 )
140141import qualified Data.IntMap.Common.Refined as Common
141142import Data.Proxy
142143import Data.Reflection
143144import Data.Traversable
144145import Data.Type.Coercion
145−import Prelude hiding (lookup, null)
146+import Prelude hiding (lookup, null, zipWith)
146147import Refined
147148import Refined.Unsafe
148149
src/Data/Map/Common/Refined.hs+10−9
@@ -20,6 +20,7 @@ import qualified Data.Set as Set
2020import Data.Traversable.WithIndex
2121import Data.Type.Coercion
2222import Data.Type.Equality ((:~:)(..))
23+import Prelude hiding (zipWith)
2324import Refined
2425import Refined.Unsafe
2526import Unsafe.Coerce
@@ -259,17 +260,17 @@ disjoint (Map m1) (Map m2)
259260
260261-- | Given two maps proven to have the same keys, for each key apply the
261262-- function to the associated values, to obtain a new map with the same keys.
262−zipWithKey
263+zipWith
263264 :: forall s k a b c. Ord k
264− => (Key s k -> a -> b -> c) -> Map s k a -> Map s k b -> Map s k c
265−zipWithKey f (Map m1) (Map m2) = Map
266− $ Map.mergeWithKey (\k x y -> Just $ f (unsafeKey k) x y)
265+ => (a -> b -> c) -> Map s k a -> Map s k b -> Map s k c
266+zipWith f (Map m1) (Map m2) = Map
267+ $ Map.mergeWithKey (\_ x y -> Just $ f x y)
267268 (\m -> if Map.null m
268269 then Map.empty
269− else error "zipWithKey: bug: Data.Map.Refined has been subverted")
270+ else error "zipWith: bug: Data.Map.Refined has been subverted")
270271 (\m -> if Map.null m
271272 then Map.empty
272− else error "zipWithKey: bug: Data.Map.Refined has been subverted")
273+ else error "zipWith: bug: Data.Map.Refined has been subverted")
273274 -- ^ Work around https://github.com/haskell/containers/issues/979
274275 m1
275276 m2
@@ -519,10 +520,10 @@ instance TraversableWithIndex (Key s k) (Map s k) where
519520 itraverse = traverseWithKey
520521
521522-- | Similar to the instance for functions -- zip corresponding keys. To use
522−-- '<*>'/'Control.Applicative.liftA2' without 'KnownSet' see 'zipWithKey'.
523+-- '<*>'/'Control.Applicative.liftA2' without 'KnownSet' see 'zipWith'.
523524instance (Ord k, KnownSet s k) => Applicative (Map s k) where
524525 pure x = fromSet \_ -> x
525− (<*>) = zipWithKey (const id)
526+ (<*>) = zipWith id
526527
527528-- | @'bind' m f@ is a map that for each key @k :: 'Key' s k@, contains the
528529-- value @f (m '!' k) '!' k@, similar to @'>>='@ for functions.
@@ -542,7 +543,7 @@ instance (Ord k, KnownSet s k) => MonadReader (Key s k) (Map s k) where
542543
543544-- | Append the values at the corresponding keys
544545instance (Ord k, Semigroup a) => Semigroup (Map s k a) where
545− (<>) = zipWithKey (const (<>))
546+ (<>) = zipWith (<>)
546547
547548instance (Ord k, KnownSet s k, Monoid a) => Monoid (Map s k a) where
548549 mempty = fromSet \_ -> mempty
src/Data/Map/Refined.hs+20−2
@@ -78,6 +78,7 @@ module Data.Map.Refined
7878 , Common.disjoint
7979 , DisjointProof(..)
8080 -- * Combine
81+ , zipWith
8182 , zipWithKey
8283 , bind
8384 , Common.union
@@ -144,12 +145,12 @@ import Data.Functor
144145import qualified Data.Map as Map
145146import Data.Map.Common.Refined
146147 ( Map(..), Key, unsafeCastKey, unsafeKey, SomeMapWith(..), Some2MapWith(..)
147− , fromSet, (!), zipWithKey, mapWithKey, traverseWithKey, bind
148+ , fromSet, (!), zipWith, mapWithKey, traverseWithKey, bind
148149 )
149150import qualified Data.Map.Common.Refined as Common
150151import Data.Traversable
151152import Data.Type.Coercion
152−import Prelude hiding (lookup, null)
153+import Prelude hiding (lookup, null, zipWith)
153154import Refined
154155import Refined.Unsafe
155156
@@ -337,6 +338,23 @@ updateLookupWithKey f k (Map m)
337338 (v', !m') -> ((unsafeKey k,) <$> v',)
338339 $ SomeMapWith (Map m') $ SupersetProof unsafeSubset
339340
341+-- | Given two maps proven to have the same keys, for each key apply the
342+-- function to the associated values, to obtain a new map with the same keys.
343+zipWithKey
344+ :: forall s k a b c. Ord k
345+ => (Key s k -> a -> b -> c) -> Map s k a -> Map s k b -> Map s k c
346+zipWithKey f (Map m1) (Map m2) = Map
347+ $ Map.mergeWithKey (\k x y -> Just $ f (unsafeKey k) x y)
348+ (\m -> if Map.null m
349+ then Map.empty
350+ else error "zipWithKey: bug: Data.Map.Refined has been subverted")
351+ (\m -> if Map.null m
352+ then Map.empty
353+ else error "zipWithKey: bug: Data.Map.Refined has been subverted")
354+ -- ^ Work around https://github.com/haskell/containers/issues/979
355+ m1
356+ m2
357+
340358-- | Return the union of two maps, with a given combining function for keys that
341359-- exist in both maps simultaneously.
342360unionWith