git.iko.soy/typeable/refined-containers
on main help sign in
Add insertWith and insertWithKey
commit7788f4a236c90d501dfb7235878bbb765b0ba339 authormniip <mniip@mniip.com> dateSun 2 Mar 2025, 14:07 (2 years ago) parentf44eb7b
6 files changed+180
src/Data/HashMap/Refined.hs+31 src/Data/HashMap/Strict/Refined.hs+31 src/Data/IntMap/Refined.hs+29 src/Data/IntMap/Strict/Refined.hs+29 src/Data/Map/Refined.hs+30 src/Data/Map/Strict/Refined.hs+30
src/Data/HashMap/Refined.hs+31
@@ -50,6 +50,8 @@ module Data.HashMap.Refined
5050 , FromTraversableProof(..)
5151 -- * Insertion
5252 , insert
53+ , insertWith
54+ , insertWithKey
5355 , InsertProof(..)
5456 , reinsert
5557 , insertLookupWithKey
@@ -198,6 +200,35 @@ insert
198200insert k v (HashMap m) = SomeHashMapWith (HashMap $ HashMap.insert k v m)
199201 $ InsertProof (unsafeKey k) unsafeSubset
200202
203+-- | Insert a key-value pair into the map to obtain a potentially larger map,
204+-- guaranteed to contain the given key. If the key was already present, the
205+-- supplied function is used to combine the new value with the old (in that
206+-- order).
207+insertWith
208+ :: forall s k a. Hashable k
209+ => (a -> a -> a)
210+ -> k
211+ -> a
212+ -> HashMap s k a
213+ -> SomeHashMapWith (InsertProof 'Hashed k s) k a
214+insertWith f k v (HashMap m) = SomeHashMapWith
215+ (HashMap $ HashMap.insertWith f k v m)
216+ $ InsertProof (unsafeKey k) unsafeSubset
217+
218+-- | Insert a key-value pair into the map to obtain a potentially larger map,
219+-- guaranteed to contain the given key. Like 'insertWith', but the combining
220+-- function has access to the key, which is guaranteed to be in the old map.
221+insertWithKey
222+ :: forall s k a. Hashable k
223+ => (Key s k -> a -> a -> a)
224+ -> k
225+ -> a
226+ -> HashMap s k a
227+ -> SomeHashMapWith (InsertProof 'Hashed k s) k a
228+insertWithKey f k v (HashMap m) = SomeHashMapWith
229+ (HashMap $ HashMap.insertWith (f $ unsafeKey k) k v m)
230+ $ InsertProof (unsafeKey k) unsafeSubset
231+
201232-- | Overwrite a key-value pair that is known to already be in the map. The set
202233-- of keys remains the same.
203234reinsert
src/Data/HashMap/Strict/Refined.hs+31
@@ -50,6 +50,8 @@ module Data.HashMap.Strict.Refined
5050 , FromTraversableProof(..)
5151 -- * Insertion
5252 , insert
53+ , insertWith
54+ , insertWithKey
5355 , InsertProof(..)
5456 , reinsert
5557 , insertLookupWithKey
@@ -206,6 +208,35 @@ insert
206208insert k v (HashMap m) = SomeHashMapWith (HashMap $ HashMap.insert k v m)
207209 $ InsertProof (unsafeKey k) unsafeSubset
208210
211+-- | Insert a key-value pair into the map to obtain a potentially larger map,
212+-- guaranteed to contain the given key. If the key was already present, the
213+-- supplied function is used to combine the new value with the old (in that
214+-- order).
215+insertWith
216+ :: forall s k a. Hashable k
217+ => (a -> a -> a)
218+ -> k
219+ -> a
220+ -> HashMap s k a
221+ -> SomeHashMapWith (InsertProof 'Hashed k s) k a
222+insertWith f k v (HashMap m) = SomeHashMapWith
223+ (HashMap $ HashMap.insertWith f k v m)
224+ $ InsertProof (unsafeKey k) unsafeSubset
225+
226+-- | Insert a key-value pair into the map to obtain a potentially larger map,
227+-- guaranteed to contain the given key. Like 'insertWith', but the combining
228+-- function has access to the key, which is guaranteed to be in the old map.
229+insertWithKey
230+ :: forall s k a. Hashable k
231+ => (Key s k -> a -> a -> a)
232+ -> k
233+ -> a
234+ -> HashMap s k a
235+ -> SomeHashMapWith (InsertProof 'Hashed k s) k a
236+insertWithKey f k v (HashMap m) = SomeHashMapWith
237+ (HashMap $ HashMap.insertWith (f $ unsafeKey k) k v m)
238+ $ InsertProof (unsafeKey k) unsafeSubset
239+
209240-- | Overwrite a key-value pair that is known to already be in the map. The set
210241-- of keys remains the same.
211242reinsert
src/Data/IntMap/Refined.hs+29
@@ -41,6 +41,8 @@ module Data.IntMap.Refined
4141 , FromTraversableProof(..)
4242 -- * Insertion
4343 , insert
44+ , insertWith
45+ , insertWithKey
4446 , InsertProof(..)
4547 , reinsert
4648 , insertLookupWithKey
@@ -202,6 +204,33 @@ insert
202204insert k v (IntMap m) = SomeIntMapWith (IntMap $ IntMap.insert k v m)
203205 $ InsertProof (unsafeKey k) unsafeSubset
204206
207+-- | Insert a key-value pair into the map to obtain a potentially larger map,
208+-- guaranteed to contain the given key. If the key was already present, the
209+-- supplied function is used to combine the new value with the old (in that
210+-- order).
211+insertWith
212+ :: forall s a. (a -> a -> a)
213+ -> Int
214+ -> a
215+ -> IntMap s a
216+ -> SomeIntMapWith (InsertProof 'Int Int s) a
217+insertWith f k v (IntMap m) = SomeIntMapWith
218+ (IntMap $ IntMap.insertWith f k v m)
219+ $ InsertProof (unsafeKey k) unsafeSubset
220+
221+-- | Insert a key-value pair into the map to obtain a potentially larger map,
222+-- guaranteed to contain the given key. Like 'insertWith', but the combining
223+-- function has access to the key, which is guaranteed to be in the old map.
224+insertWithKey
225+ :: forall s a. (Key s -> a -> a -> a)
226+ -> Int
227+ -> a
228+ -> IntMap s a
229+ -> SomeIntMapWith (InsertProof 'Int Int s) a
230+insertWithKey f k v (IntMap m) = SomeIntMapWith
231+ (IntMap $ IntMap.insertWithKey (f . unsafeKey) k v m)
232+ $ InsertProof (unsafeKey k) unsafeSubset
233+
205234-- | Overwrite a key-value pair that is known to already be in the map. The set
206235-- of keys remains the same.
207236reinsert
src/Data/IntMap/Strict/Refined.hs+29
@@ -41,6 +41,8 @@ module Data.IntMap.Strict.Refined
4141 , FromTraversableProof(..)
4242 -- * Insertion
4343 , insert
44+ , insertWith
45+ , insertWithKey
4446 , InsertProof(..)
4547 , reinsert
4648 , insertLookupWithKey
@@ -208,6 +210,33 @@ insert
208210insert k v (IntMap m) = SomeIntMapWith (IntMap $ IntMap.insert k v m)
209211 $ InsertProof (unsafeKey k) unsafeSubset
210212
213+-- | Insert a key-value pair into the map to obtain a potentially larger map,
214+-- guaranteed to contain the given key. If the key was already present, the
215+-- supplied function is used to combine the new value with the old (in that
216+-- order).
217+insertWith
218+ :: forall s a. (a -> a -> a)
219+ -> Int
220+ -> a
221+ -> IntMap s a
222+ -> SomeIntMapWith (InsertProof 'Int Int s) a
223+insertWith f k v (IntMap m) = SomeIntMapWith
224+ (IntMap $ IntMap.insertWith f k v m)
225+ $ InsertProof (unsafeKey k) unsafeSubset
226+
227+-- | Insert a key-value pair into the map to obtain a potentially larger map,
228+-- guaranteed to contain the given key. Like 'insertWith', but the combining
229+-- function has access to the key, which is guaranteed to be in the old map.
230+insertWithKey
231+ :: forall s a. (Key s -> a -> a -> a)
232+ -> Int
233+ -> a
234+ -> IntMap s a
235+ -> SomeIntMapWith (InsertProof 'Int Int s) a
236+insertWithKey f k v (IntMap m) = SomeIntMapWith
237+ (IntMap $ IntMap.insertWithKey (f . unsafeKey) k v m)
238+ $ InsertProof (unsafeKey k) unsafeSubset
239+
211240-- | Overwrite a key-value pair that is known to already be in the map. The set
212241-- of keys remains the same.
213242reinsert
src/Data/Map/Refined.hs+30
@@ -50,6 +50,8 @@ module Data.Map.Refined
5050 , FromTraversableProof(..)
5151 -- * Insertion
5252 , insert
53+ , insertWith
54+ , insertWithKey
5355 , InsertProof(..)
5456 , reinsert
5557 , insertLookupWithKey
@@ -206,6 +208,34 @@ insert
206208insert k v (Map m) = SomeMapWith (Map $ Map.insert k v m)
207209 $ InsertProof (unsafeKey k) unsafeSubset
208210
211+-- | Insert a key-value pair into the map to obtain a potentially larger map,
212+-- guaranteed to contain the given key. If the key was already present, the
213+-- supplied function is used to combine the new value with the old (in that
214+-- order).
215+insertWith
216+ :: forall s k a. Ord k
217+ => (a -> a -> a)
218+ -> k
219+ -> a
220+ -> Map s k a
221+ -> SomeMapWith (InsertProof 'Regular k s) k a
222+insertWith f k v (Map m) = SomeMapWith (Map $ Map.insertWith f k v m)
223+ $ InsertProof (unsafeKey k) unsafeSubset
224+
225+-- | Insert a key-value pair into the map to obtain a potentially larger map,
226+-- guaranteed to contain the given key. Like 'insertWith', but the combining
227+-- function has access to the key, which is guaranteed to be in the old map.
228+insertWithKey
229+ :: forall s k a. Ord k
230+ => (Key s k -> a -> a -> a)
231+ -> k
232+ -> a
233+ -> Map s k a
234+ -> SomeMapWith (InsertProof 'Regular k s) k a
235+insertWithKey f k v (Map m) = SomeMapWith
236+ (Map $ Map.insertWithKey (f . unsafeKey) k v m)
237+ $ InsertProof (unsafeKey k) unsafeSubset
238+
209239-- | Overwrite a key-value pair that is known to already be in the map. The set
210240-- of keys remains the same.
211241reinsert
src/Data/Map/Strict/Refined.hs+30
@@ -50,6 +50,8 @@ module Data.Map.Strict.Refined
5050 , FromTraversableProof(..)
5151 -- * Insertion
5252 , insert
53+ , insertWith
54+ , insertWithKey
5355 , InsertProof(..)
5456 , reinsert
5557 , insertLookupWithKey
@@ -213,6 +215,34 @@ insert
213215insert k v (Map m) = SomeMapWith (Map $ Map.insert k v m)
214216 $ InsertProof (unsafeKey k) unsafeSubset
215217
218+-- | Insert a key-value pair into the map to obtain a potentially larger map,
219+-- guaranteed to contain the given key. If the key was already present, the
220+-- supplied function is used to combine the new value with the old (in that
221+-- order).
222+insertWith
223+ :: forall s k a. Ord k
224+ => (a -> a -> a)
225+ -> k
226+ -> a
227+ -> Map s k a
228+ -> SomeMapWith (InsertProof 'Regular k s) k a
229+insertWith f k v (Map m) = SomeMapWith (Map $ Map.insertWith f k v m)
230+ $ InsertProof (unsafeKey k) unsafeSubset
231+
232+-- | Insert a key-value pair into the map to obtain a potentially larger map,
233+-- guaranteed to contain the given key. Like 'insertWith', but the combining
234+-- function has access to the key, which is guaranteed to be in the old map.
235+insertWithKey
236+ :: forall s k a. Ord k
237+ => (Key s k -> a -> a -> a)
238+ -> k
239+ -> a
240+ -> Map s k a
241+ -> SomeMapWith (InsertProof 'Regular k s) k a
242+insertWithKey f k v (Map m) = SomeMapWith
243+ (Map $ Map.insertWithKey (f . unsafeKey) k v m)
244+ $ InsertProof (unsafeKey k) unsafeSubset
245+
216246-- | Overwrite a key-value pair that is known to already be in the map. The set
217247-- of keys remains the same.
218248reinsert