git.iko.soy/typeable/refined-containers
on main help sign in
Add intersection and intersectionWith
commita959064811940c9179b0fd6cb11d319dbda23878 authormniip <mniip@mniip.com> dateWed 5 Mar 2025, 13:05 (2 years ago) parentc9496f3
9 files changed+117
src/Data/HashMap/Common/Refined.hs+14 src/Data/HashMap/Refined.hs+13 src/Data/HashMap/Strict/Refined.hs+13 src/Data/IntMap/Common/Refined.hs+13 src/Data/IntMap/Refined.hs+12 src/Data/IntMap/Strict/Refined.hs+12 src/Data/Map/Common/Refined.hs+14 src/Data/Map/Refined.hs+13 src/Data/Map/Strict/Refined.hs+13
src/Data/HashMap/Common/Refined.hs+14
@@ -270,6 +270,20 @@ difference (HashMap m1) (HashMap m2)
270270 = SomeHashMapWith (HashMap $ HashMap.difference m1 m2)
271271 $ DifferenceProof unsafeSubset (\f g -> unsafeSubsetWith2 f g) unsafeSubset
272272
273+-- | Return the intersection of two maps, taking values from the first map.
274+--
275+-- @
276+-- 'intersection' = intersectionWith 'const'
277+-- @
278+intersection
279+ :: forall s t k a b. Hashable k
280+ => HashMap s k a
281+ -> HashMap t k b
282+ -> SomeHashMapWith (IntersectionProof 'Hashed s t) k a
283+intersection (HashMap m1) (HashMap m2) = SomeHashMapWith
284+ (HashMap $ HashMap.intersection m1 m2)
285+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
286+
273287-- | Apply a function to all values in a map, together with their corresponding
274288-- keys, that are proven to be in the map. The set of keys remains the same.
275289mapWithKey
src/Data/HashMap/Refined.hs+13
@@ -85,6 +85,8 @@ module Data.HashMap.Refined
8585 , differenceWith
8686 , differenceWithKey
8787 , PartialDifferenceProof(..)
88+ , Common.intersection
89+ , intersectionWith
8890 , intersectionWithKey
8991 , IntersectionProof(..)
9092 -- * Traversal
@@ -390,6 +392,17 @@ differenceWithKey f (HashMap m1) (HashMap m2) = SomeHashMapWith
390392 (HashMap.mapWithKey (,) m2))
391393 $ PartialDifferenceProof unsafeSubset unsafeSubset
392394
395+-- | Return the intersection of two maps with the given combining function.
396+intersectionWith
397+ :: forall s t k a b c. Hashable k
398+ => (a -> b -> c)
399+ -> HashMap s k a
400+ -> HashMap t k b
401+ -> SomeHashMapWith (IntersectionProof 'Hashed s t) k c
402+intersectionWith f (HashMap m1) (HashMap m2) = SomeHashMapWith
403+ (HashMap $ HashMap.intersectionWith f m1 m2)
404+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
405+
393406-- | Return the intersection of two maps with the given combining function.
394407--
395408-- You can use 'andLeft' and 'andRight' to obtain @'Key' s k@ and @'Key' t k@
src/Data/HashMap/Strict/Refined.hs+13
@@ -85,6 +85,8 @@ module Data.HashMap.Strict.Refined
8585 , differenceWith
8686 , differenceWithKey
8787 , PartialDifferenceProof(..)
88+ , Common.intersection
89+ , intersectionWith
8890 , intersectionWithKey
8991 , IntersectionProof(..)
9092 -- * Traversal
@@ -406,6 +408,17 @@ differenceWithKey f (HashMap m1) (HashMap m2) = SomeHashMapWith
406408 (HashMap.mapWithKey (,) m2))
407409 $ PartialDifferenceProof unsafeSubset unsafeSubset
408410
411+-- | Return the intersection of two maps with the given combining function.
412+intersectionWith
413+ :: forall s t k a b c. Hashable k
414+ => (a -> b -> c)
415+ -> HashMap s k a
416+ -> HashMap t k b
417+ -> SomeHashMapWith (IntersectionProof 'Hashed s t) k c
418+intersectionWith f (HashMap m1) (HashMap m2) = SomeHashMapWith
419+ (HashMap $ HashMap.intersectionWith f m1 m2)
420+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
421+
409422-- | Return the intersection of two maps with the given combining function.
410423--
411424-- You can use 'andLeft' and 'andRight' to obtain @'Key' s k@ and @'Key' t k@
src/Data/IntMap/Common/Refined.hs+13
@@ -288,6 +288,19 @@ difference (IntMap m1) (IntMap m2) = SomeIntMapWith
288288 (IntMap $ IntMap.difference m1 m2)
289289 $ DifferenceProof unsafeSubset (\f g -> unsafeSubsetWith2 f g) unsafeSubset
290290
291+-- | Return the intersection of two maps, taking values from the first map.
292+--
293+-- @
294+-- 'intersection' = intersectionWith 'const'
295+-- @
296+intersection
297+ :: forall s t a b. IntMap s a
298+ -> IntMap t b
299+ -> SomeIntMapWith (IntersectionProof 'Int s t) a
300+intersection (IntMap m1) (IntMap m2) = SomeIntMapWith
301+ (IntMap $ IntMap.intersection m1 m2)
302+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
303+
291304-- | Apply a function to all values in a map, together with their corresponding
292305-- keys, that are proven to be in the map. The set of keys remains the same.
293306mapWithKey :: forall s a b. (Key s -> a -> b) -> IntMap s a -> IntMap s b
src/Data/IntMap/Refined.hs+12
@@ -80,6 +80,8 @@ module Data.IntMap.Refined
8080 , differenceWith
8181 , differenceWithKey
8282 , PartialDifferenceProof(..)
83+ , Common.intersection
84+ , intersectionWith
8385 , intersectionWithKey
8486 , IntersectionProof(..)
8587 -- * Traversal
@@ -377,6 +379,16 @@ differenceWithKey f (IntMap m1) (IntMap m2) = SomeIntMapWith
377379 (IntMap $ IntMap.differenceWithKey (f . reallyUnsafeRefine) m1 m2)
378380 $ PartialDifferenceProof unsafeSubset unsafeSubset
379381
382+-- | Return the intersection of two maps with the given combining function.
383+intersectionWith
384+ :: forall s t a b c. (a -> b -> c)
385+ -> IntMap s a
386+ -> IntMap t b
387+ -> SomeIntMapWith (IntersectionProof 'Int s t) c
388+intersectionWith f (IntMap m1) (IntMap m2) = SomeIntMapWith
389+ (IntMap $ IntMap.intersectionWith f m1 m2)
390+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
391+
380392-- | Return the intersection of two maps with the given combining function.
381393--
382394-- You can use 'andLeft' and 'andRight' to obtain @'Key' s@ and @'Key' t@
src/Data/IntMap/Strict/Refined.hs+12
@@ -80,6 +80,8 @@ module Data.IntMap.Strict.Refined
8080 , differenceWith
8181 , differenceWithKey
8282 , PartialDifferenceProof(..)
83+ , Common.intersection
84+ , intersectionWith
8385 , intersectionWithKey
8486 , IntersectionProof(..)
8587 -- * Traversal
@@ -404,6 +406,16 @@ differenceWithKey f (IntMap m1) (IntMap m2) = SomeIntMapWith
404406 (IntMap $ IntMap.differenceWithKey (f . reallyUnsafeRefine) m1 m2)
405407 $ PartialDifferenceProof unsafeSubset unsafeSubset
406408
409+-- | Return the intersection of two maps with the given combining function.
410+intersectionWith
411+ :: forall s t a b c. (a -> b -> c)
412+ -> IntMap s a
413+ -> IntMap t b
414+ -> SomeIntMapWith (IntersectionProof 'Int s t) c
415+intersectionWith f (IntMap m1) (IntMap m2) = SomeIntMapWith
416+ (IntMap $ IntMap.intersectionWith f m1 m2)
417+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
418+
407419-- | Return the intersection of two maps with the given combining function.
408420--
409421-- You can use 'andLeft' and 'andRight' to obtain @'Key' s@ and @'Key' t@
src/Data/Map/Common/Refined.hs+14
@@ -293,6 +293,20 @@ difference
293293difference (Map m1) (Map m2) = SomeMapWith (Map $ Map.difference m1 m2)
294294 $ DifferenceProof unsafeSubset (\f g -> unsafeSubsetWith2 f g) unsafeSubset
295295
296+-- | Return the intersection of two maps, taking values from the first map.
297+--
298+-- @
299+-- 'intersection' = intersectionWith 'const'
300+-- @
301+intersection
302+ :: forall s t k a b. Ord k
303+ => Map s k a
304+ -> Map t k b
305+ -> SomeMapWith (IntersectionProof 'Regular s t) k a
306+intersection (Map m1) (Map m2)
307+ = SomeMapWith (Map $ Map.intersection m1 m2)
308+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
309+
296310-- | Apply a function to all values in a map, together with their corresponding
297311-- keys, that are proven to be in the map. The set of keys remains the same.
298312mapWithKey :: forall s k a b. (Key s k -> a -> b) -> Map s k a -> Map s k b
src/Data/Map/Refined.hs+13
@@ -89,6 +89,8 @@ module Data.Map.Refined
8989 , differenceWith
9090 , differenceWithKey
9191 , PartialDifferenceProof(..)
92+ , Common.intersection
93+ , intersectionWith
9294 , intersectionWithKey
9395 , IntersectionProof(..)
9496 -- * Traversal
@@ -388,6 +390,17 @@ differenceWithKey f (Map m1) (Map m2)
388390 = SomeMapWith (Map $ Map.differenceWithKey (f . reallyUnsafeRefine) m1 m2)
389391 $ PartialDifferenceProof unsafeSubset unsafeSubset
390392
393+-- | Return the intersection of two maps with the given combining function.
394+intersectionWith
395+ :: forall s t k a b c. Ord k
396+ => (a -> b -> c)
397+ -> Map s k a
398+ -> Map t k b
399+ -> SomeMapWith (IntersectionProof 'Regular s t) k c
400+intersectionWith f (Map m1) (Map m2)
401+ = SomeMapWith (Map $ Map.intersectionWith f m1 m2)
402+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
403+
391404-- | Return the intersection of two maps with the given combining function.
392405--
393406-- You can use 'andLeft' and 'andRight' to obtain @'Key' s k@ and @'Key' t k@
src/Data/Map/Strict/Refined.hs+13
@@ -89,6 +89,8 @@ module Data.Map.Strict.Refined
8989 , differenceWith
9090 , differenceWithKey
9191 , PartialDifferenceProof(..)
92+ , Common.intersection
93+ , intersectionWith
9294 , intersectionWithKey
9395 , IntersectionProof(..)
9496 -- * Traversal
@@ -412,6 +414,17 @@ differenceWithKey f (Map m1) (Map m2)
412414 = SomeMapWith (Map $ Map.differenceWithKey (f . reallyUnsafeRefine) m1 m2)
413415 $ PartialDifferenceProof unsafeSubset unsafeSubset
414416
417+-- | Return the intersection of two maps with the given combining function.
418+intersectionWith
419+ :: forall s t k a b c. Ord k
420+ => (a -> b -> c)
421+ -> Map s k a
422+ -> Map t k b
423+ -> SomeMapWith (IntersectionProof 'Regular s t) k c
424+intersectionWith f (Map m1) (Map m2)
425+ = SomeMapWith (Map $ Map.intersectionWith f m1 m2)
426+ $ IntersectionProof unsafeSubset unsafeSubsetWith2
427+
415428-- | Return the intersection of two maps with the given combining function.
416429--
417430-- You can use 'andLeft' and 'andRight' to obtain @'Key' s k@ and @'Key' t k@