git.iko.soy/typeable/refined-containers
on main help sign in
Add adjust', update', updateWithKey
3fab77cmniip
src/Data/HashMap/Refined.hs+39−4
@@ -57,9 +57,12 @@ module Data.HashMap.Refined
57 , insertLookupWithKey
57 , insertLookupWithKey
58 -- * Deletion/Update
58 -- * Deletion/Update
59 , Common.delete
59 , Common.delete
60+ , adjust'
60 , adjust
61 , adjust
61 , adjustWithKey
62 , adjustWithKey
63+ , update'
62 , update
64 , update
65+ , updateWithKey
63 , updateLookupWithKey
66 , updateLookupWithKey
64 -- * Query
67 -- * Query
65 , Common.lookup
68 , Common.lookup
@@ -252,12 +255,22 @@ insertLookupWithKey f k v (HashMap m) =
252 $ InsertProof (unsafeKey k) unsafeSubset
255 $ InsertProof (unsafeKey k) unsafeSubset
253 )
256 )
254
257
258+-- | If the given key is in the map, update the value at that key using the
259+-- given function. In any case, the set of keys remains the same.
260+adjust'
261+ :: forall s k a. Hashable k => (a -> a) -> k -> HashMap s k a -> HashMap s k a
262+adjust' = coerce $ HashMap.adjust @k @a
263+
255-- | Update the value at a specific key known the be in the map using the given
264-- | Update the value at a specific key known the be in the map using the given
256-- function. The set of keys remains the same.
265-- function. The set of keys remains the same.
266+--
267+-- @
268+-- 'reinsert' k v = 'adjust (const v) k'
269+-- @
257adjust
270adjust
258 :: forall s k a. Hashable k
271 :: forall s k a. Hashable k
259 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
272 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
260−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ HashMap.adjust @k @a
273+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
261
274
262-- | If the given key is in the map, update the associated value using the given
275-- | If the given key is in the map, update the associated value using the given
263-- function with a proof that the key was in the map; otherwise return the map
276-- function with a proof that the key was in the map; otherwise return the map
@@ -267,6 +280,17 @@ adjustWithKey
267 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
280 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
268adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
281adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
269
282
283+-- | If a key is present in the map, update its value or delete it using the
284+-- given function, returning a potentially smaller map.
285+update'
286+ :: forall s k a. Hashable k
287+ => (a -> Maybe a)
288+ -> k
289+ -> HashMap s k a
290+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
291+update' f k (HashMap m) = SomeHashMapWith (HashMap $ HashMap.update f k m)
292+ $ SupersetProof unsafeSubset
293+
270-- | Update or delete a key known to be in the map using the given function,
294-- | Update or delete a key known to be in the map using the given function,
271-- returning a potentially smaller map.
295-- returning a potentially smaller map.
272update
296update
@@ -275,9 +299,20 @@ update
275 -> Key s k
299 -> Key s k
276 -> HashMap s k a
300 -> HashMap s k a
277 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
301 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
278−update f k (HashMap m)
302+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
303+
304+-- | If a key is present in the map, update its value or delete it using the
305+-- given function with a proof that the key was in the map, returning a
306+-- potentially smaller map.
307+updateWithKey
308+ :: forall s k a. Hashable k
309+ => (Key s k -> a -> Maybe a)
310+ -> k
311+ -> HashMap s k a
312+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
313+updateWithKey f k (HashMap m) = SomeHashMapWith
279− = SomeHashMapWith (HashMap $ HashMap.update f (unrefine k) m)
314+ (HashMap $ HashMap.update (f $ unsafeKey k) k m)
280− $ SupersetProof unsafeSubset
315+ $ SupersetProof unsafeSubset
281
316
282-- | If the given key is in the map, update or delete it using the given
317-- | If the given key is in the map, update or delete it using the given
283-- function with a proof that the key was in the map; otherwise the map is
318-- function with a proof that the key was in the map; otherwise the map is
src/Data/HashMap/Strict/Refined.hs+39−4
@@ -57,9 +57,12 @@ module Data.HashMap.Strict.Refined
57 , insertLookupWithKey
57 , insertLookupWithKey
58 -- * Deletion/Update
58 -- * Deletion/Update
59 , Common.delete
59 , Common.delete
60+ , adjust'
60 , adjust
61 , adjust
61 , adjustWithKey
62 , adjustWithKey
63+ , update'
62 , update
64 , update
65+ , updateWithKey
63 , updateLookupWithKey
66 , updateLookupWithKey
64 -- * Query
67 -- * Query
65 , Common.lookup
68 , Common.lookup
@@ -239,6 +242,10 @@ insertWithKey f k v (HashMap m) = SomeHashMapWith
239
242
240-- | Overwrite a key-value pair that is known to already be in the map. The set
243-- | Overwrite a key-value pair that is known to already be in the map. The set
241-- of keys remains the same.
244-- of keys remains the same.
245+--
246+-- @
247+-- 'reinsert' k v = 'adjust (const v) k'
248+-- @
242reinsert
249reinsert
243 :: forall s k a. Hashable k
250 :: forall s k a. Hashable k
244 => Key s k -> a -> HashMap s k a -> HashMap s k a
251 => Key s k -> a -> HashMap s k a -> HashMap s k a
@@ -260,12 +267,18 @@ insertLookupWithKey f k v (HashMap m) =
260 $ InsertProof (unsafeKey k) unsafeSubset
267 $ InsertProof (unsafeKey k) unsafeSubset
261 )
268 )
262
269
270+-- | If the given key is in the map, update the value at that key using the
271+-- given function. In any case, the set of keys remains the same.
272+adjust'
273+ :: forall s k a. Hashable k => (a -> a) -> k -> HashMap s k a -> HashMap s k a
274+adjust' = coerce $ HashMap.adjust @k @a
275+
263-- | Update the value at a specific key known the be in the map using the given
276-- | Update the value at a specific key known the be in the map using the given
264-- function. The set of keys remains the same.
277-- function. The set of keys remains the same.
265adjust
278adjust
266 :: forall s k a. Hashable k
279 :: forall s k a. Hashable k
267 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
280 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
268−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ HashMap.adjust @k @a
281+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
269
282
270-- | If the given key is in the map, update the associated value using the given
283-- | If the given key is in the map, update the associated value using the given
271-- function with a proof that the key was in the map; otherwise return the map
284-- function with a proof that the key was in the map; otherwise return the map
@@ -275,6 +288,17 @@ adjustWithKey
275 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
288 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
276adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
289adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
277
290
291+-- | If a key is present in the map, update its value or delete it using the
292+-- given function, returning a potentially smaller map.
293+update'
294+ :: forall s k a. Hashable k
295+ => (a -> Maybe a)
296+ -> k
297+ -> HashMap s k a
298+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
299+update' f k (HashMap m) = SomeHashMapWith (HashMap $ HashMap.update f k m)
300+ $ SupersetProof unsafeSubset
301+
278-- | Update or delete a key known to be in the map using the given function,
302-- | Update or delete a key known to be in the map using the given function,
279-- returning a potentially smaller map.
303-- returning a potentially smaller map.
280update
304update
@@ -283,9 +307,20 @@ update
283 -> Key s k
307 -> Key s k
284 -> HashMap s k a
308 -> HashMap s k a
285 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
309 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
286−update f k (HashMap m)
310+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
311+
312+-- | If a key is present in the map, update its value or delete it using the
313+-- given function with a proof that the key was in the map, returning a
314+-- potentially smaller map.
315+updateWithKey
316+ :: forall s k a. Hashable k
317+ => (Key s k -> a -> Maybe a)
318+ -> k
319+ -> HashMap s k a
320+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
321+updateWithKey f k (HashMap m) = SomeHashMapWith
287− = SomeHashMapWith (HashMap $ HashMap.update f (unrefine k) m)
322+ (HashMap $ HashMap.update (f $ unsafeKey k) k m)
288− $ SupersetProof unsafeSubset
323+ $ SupersetProof unsafeSubset
289
324
290-- | If the given key is in the map, update or delete it using the given
325-- | If the given key is in the map, update or delete it using the given
291-- function with a proof that the key was in the map; otherwise the map is
326-- function with a proof that the key was in the map; otherwise the map is
src/Data/IntMap/Refined.hs+35−2
@@ -48,9 +48,12 @@ module Data.IntMap.Refined
48 , insertLookupWithKey
48 , insertLookupWithKey
49 -- * Deletion/Update
49 -- * Deletion/Update
50 , Common.delete
50 , Common.delete
51+ , adjust'
51 , adjust
52 , adjust
52 , adjustWithKey
53 , adjustWithKey
54+ , update'
53 , update
55 , update
56+ , updateWithKey
54 , updateLookupWithKey
57 , updateLookupWithKey
55 -- * Query
58 -- * Query
56 , Common.lookup
59 , Common.lookup
@@ -251,10 +254,19 @@ insertLookupWithKey f k v (IntMap m)
251 (v', !m') -> ((unsafeKey k,) <$> v',)
254 (v', !m') -> ((unsafeKey k,) <$> v',)
252 $ SomeIntMapWith (IntMap m') $ InsertProof (unsafeKey k) unsafeSubset
255 $ SomeIntMapWith (IntMap m') $ InsertProof (unsafeKey k) unsafeSubset
253
256
257+-- | If the given key is in the map, update the value at that key using the
258+-- given function. In any case, the set of keys remains the same.
259+adjust' :: forall s a. (a -> a) -> Int -> IntMap s a -> IntMap s a
260+adjust' = coerce $ IntMap.adjust @a
261+
254-- | Update the value at a specific key known the be in the map using the given
262-- | Update the value at a specific key known the be in the map using the given
255-- function. The set of keys remains the same.
263-- function. The set of keys remains the same.
264+--
265+-- @
266+-- 'reinsert' k v = 'adjust (const v) k'
267+-- @
256adjust :: forall s a. (a -> a) -> Key s -> IntMap s a -> IntMap s a
268adjust :: forall s a. (a -> a) -> Key s -> IntMap s a -> IntMap s a
257−adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.adjust @a
269+adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ adjust' @s @a
258
270
259-- | If the given key is in the map, update the associated value using the given
271-- | If the given key is in the map, update the associated value using the given
260-- function with a proof that the key was in the map; otherwise return the map
272-- function with a proof that the key was in the map; otherwise return the map
@@ -264,6 +276,16 @@ adjustWithKey
264adjustWithKey = gcoerceWith (unsafeCastKey @s) $ coerce
276adjustWithKey = gcoerceWith (unsafeCastKey @s) $ coerce
265 $ IntMap.adjustWithKey @a
277 $ IntMap.adjustWithKey @a
266
278
279+-- | If a key is present in the map, update its value or delete it using the
280+-- given function, returning a potentially smaller map.
281+update'
282+ :: forall s a. (a -> Maybe a)
283+ -> Int
284+ -> IntMap s a
285+ -> SomeIntMapWith (SupersetProof 'Int s) a
286+update' f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f k m)
287+ $ SupersetProof unsafeSubset
288+
267-- | Update or delete a key known to be in the map using the given function,
289-- | Update or delete a key known to be in the map using the given function,
268-- returning a potentially smaller map.
290-- returning a potentially smaller map.
269update
291update
@@ -271,7 +293,18 @@ update
271 -> Key s
293 -> Key s
272 -> IntMap s a
294 -> IntMap s a
273 -> SomeIntMapWith (SupersetProof 'Int s) a
295 -> SomeIntMapWith (SupersetProof 'Int s) a
296+update = gcoerceWith (unsafeCastKey @s) $ coerce $ update' @s @a
297+
298+-- | If a key is present in the map, update its value or delete it using the
299+-- given function with a proof that the key was in the map, returning a
300+-- potentially smaller map.
301+updateWithKey
302+ :: forall s a. (Key s -> a -> Maybe a)
303+ -> Int
304+ -> IntMap s a
305+ -> SomeIntMapWith (SupersetProof 'Int s) a
274−update f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f (unrefine k) m)
306+updateWithKey f k (IntMap m) = SomeIntMapWith
307+ (IntMap $ IntMap.updateWithKey (f . unsafeKey) k m)
275 $ SupersetProof unsafeSubset
308 $ SupersetProof unsafeSubset
276
309
277-- | If the given key is in the map, update or delete it using the given
310-- | If the given key is in the map, update or delete it using the given
src/Data/IntMap/Strict/Refined.hs+35−2
@@ -48,9 +48,12 @@ module Data.IntMap.Strict.Refined
48 , insertLookupWithKey
48 , insertLookupWithKey
49 -- * Deletion/Update
49 -- * Deletion/Update
50 , Common.delete
50 , Common.delete
51+ , adjust'
51 , adjust
52 , adjust
52 , adjustWithKey
53 , adjustWithKey
54+ , update'
53 , update
55 , update
56+ , updateWithKey
54 , updateLookupWithKey
57 , updateLookupWithKey
55 -- * Query
58 -- * Query
56 , Common.lookup
59 , Common.lookup
@@ -239,6 +242,10 @@ insertWithKey f k v (IntMap m) = SomeIntMapWith
239
242
240-- | Overwrite a key-value pair that is known to already be in the map. The set
243-- | Overwrite a key-value pair that is known to already be in the map. The set
241-- of keys remains the same.
244-- of keys remains the same.
245+--
246+-- @
247+-- 'reinsert' k v = 'adjust (const v) k'
248+-- @
242reinsert
249reinsert
243 :: forall s a. Key s -> a -> IntMap s a -> IntMap s a
250 :: forall s a. Key s -> a -> IntMap s a -> IntMap s a
244reinsert = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.insert @a
251reinsert = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.insert @a
@@ -257,10 +264,15 @@ insertLookupWithKey f k v (IntMap m)
257 (v', !m') -> ((unsafeKey k,) <$> v',)
264 (v', !m') -> ((unsafeKey k,) <$> v',)
258 $ SomeIntMapWith (IntMap m') $ InsertProof (unsafeKey k) unsafeSubset
265 $ SomeIntMapWith (IntMap m') $ InsertProof (unsafeKey k) unsafeSubset
259
266
267+-- | If the given key is in the map, update the value at that key using the
268+-- given function. In any case, the set of keys remains the same.
269+adjust' :: forall s a. (a -> a) -> Int -> IntMap s a -> IntMap s a
270+adjust' = coerce $ IntMap.adjust @a
271+
260-- | Update the value at a specific key known the be in the map using the given
272-- | Update the value at a specific key known the be in the map using the given
261-- function. The set of keys remains the same.
273-- function. The set of keys remains the same.
262adjust :: forall s a. (a -> a) -> Key s -> IntMap s a -> IntMap s a
274adjust :: forall s a. (a -> a) -> Key s -> IntMap s a -> IntMap s a
263−adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.adjust @a
275+adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ adjust' @s @a
264
276
265-- | If the given key is in the map, update the associated value using the given
277-- | If the given key is in the map, update the associated value using the given
266-- function with a proof that the key was in the map; otherwise return the map
278-- function with a proof that the key was in the map; otherwise return the map
@@ -270,6 +282,16 @@ adjustWithKey
270adjustWithKey = gcoerceWith (unsafeCastKey @s) $ coerce
282adjustWithKey = gcoerceWith (unsafeCastKey @s) $ coerce
271 $ IntMap.adjustWithKey @a
283 $ IntMap.adjustWithKey @a
272
284
285+-- | If a key is present in the map, update its value or delete it using the
286+-- given function, returning a potentially smaller map.
287+update'
288+ :: forall s a. (a -> Maybe a)
289+ -> Int
290+ -> IntMap s a
291+ -> SomeIntMapWith (SupersetProof 'Int s) a
292+update' f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f k m)
293+ $ SupersetProof unsafeSubset
294+
273-- | Update or delete a key known to be in the map using the given function,
295-- | Update or delete a key known to be in the map using the given function,
274-- returning a potentially smaller map.
296-- returning a potentially smaller map.
275update
297update
@@ -277,7 +299,18 @@ update
277 -> Key s
299 -> Key s
278 -> IntMap s a
300 -> IntMap s a
279 -> SomeIntMapWith (SupersetProof 'Int s) a
301 -> SomeIntMapWith (SupersetProof 'Int s) a
302+update = gcoerceWith (unsafeCastKey @s) $ coerce $ update' @s @a
303+
304+-- | If a key is present in the map, update its value or delete it using the
305+-- given function with a proof that the key was in the map, returning a
306+-- potentially smaller map.
307+updateWithKey
308+ :: forall s a. (Key s -> a -> Maybe a)
309+ -> Int
310+ -> IntMap s a
311+ -> SomeIntMapWith (SupersetProof 'Int s) a
280−update f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f (unrefine k) m)
312+updateWithKey f k (IntMap m) = SomeIntMapWith
313+ (IntMap $ IntMap.updateWithKey (f . unsafeKey) k m)
281 $ SupersetProof unsafeSubset
314 $ SupersetProof unsafeSubset
282
315
283-- | If the given key is in the map, update or delete it using the given
316-- | If the given key is in the map, update or delete it using the given
src/Data/Map/Refined.hs+37−2
@@ -57,9 +57,12 @@ module Data.Map.Refined
57 , insertLookupWithKey
57 , insertLookupWithKey
58 -- * Deletion/Update
58 -- * Deletion/Update
59 , Common.delete
59 , Common.delete
60+ , adjust'
60 , adjust
61 , adjust
61 , adjustWithKey
62 , adjustWithKey
63+ , update'
62 , update
64 , update
65+ , updateWithKey
63 , updateLookupWithKey
66 , updateLookupWithKey
64 -- * Query
67 -- * Query
65 , Common.lookup
68 , Common.lookup
@@ -238,6 +241,10 @@ insertWithKey f k v (Map m) = SomeMapWith
238
241
239-- | Overwrite a key-value pair that is known to already be in the map. The set
242-- | Overwrite a key-value pair that is known to already be in the map. The set
240-- of keys remains the same.
243-- of keys remains the same.
244+--
245+-- @
246+-- 'reinsert' k v = 'adjust (const v) k'
247+-- @
241reinsert
248reinsert
242 :: forall s k a. Ord k
249 :: forall s k a. Ord k
243 => Key s k -> a -> Map s k a -> Map s k a
250 => Key s k -> a -> Map s k a -> Map s k a
@@ -258,10 +265,15 @@ insertLookupWithKey f k v (Map m)
258 (v', !m') -> ((unsafeKey k,) <$> v',)
265 (v', !m') -> ((unsafeKey k,) <$> v',)
259 $ SomeMapWith (Map m') $ InsertProof (unsafeKey k) unsafeSubset
266 $ SomeMapWith (Map m') $ InsertProof (unsafeKey k) unsafeSubset
260
267
268+-- | If the given key is in the map, update the value at that key using the
269+-- given function. In any case, the set of keys remains the same.
270+adjust' :: forall s k a. Ord k => (a -> a) -> k -> Map s k a -> Map s k a
271+adjust' = coerce $ Map.adjust @k @a
272+
261-- | Update the value at a specific key known the be in the map using the given
273-- | Update the value at a specific key known the be in the map using the given
262-- function. The set of keys remains the same.
274-- function. The set of keys remains the same.
263adjust :: forall s k a. Ord k => (a -> a) -> Key s k -> Map s k a -> Map s k a
275adjust :: forall s k a. Ord k => (a -> a) -> Key s k -> Map s k a -> Map s k a
264−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ Map.adjust @k @a
276+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
265
277
266-- | If the given key is in the map, update the associated value using the given
278-- | If the given key is in the map, update the associated value using the given
267-- function with a proof that the key was in the map; otherwise return the map
279-- function with a proof that the key was in the map; otherwise return the map
@@ -271,6 +283,17 @@ adjustWithKey
271adjustWithKey = gcoerceWith (unsafeCastKey @s @k) $ coerce
283adjustWithKey = gcoerceWith (unsafeCastKey @s @k) $ coerce
272 $ Map.adjustWithKey @k @a
284 $ Map.adjustWithKey @k @a
273
285
286+-- | If a key is present in the map, update its value or delete it using the
287+-- given function, returning a potentially smaller map.
288+update'
289+ :: forall s k a. Ord k
290+ => (a -> Maybe a)
291+ -> k
292+ -> Map s k a
293+ -> SomeMapWith (SupersetProof 'Regular s) k a
294+update' f k (Map m) = SomeMapWith (Map $ Map.update f k m)
295+ $ SupersetProof unsafeSubset
296+
274-- | Update or delete a key known to be in the map using the given function,
297-- | Update or delete a key known to be in the map using the given function,
275-- returning a potentially smaller map.
298-- returning a potentially smaller map.
276update
299update
@@ -279,7 +302,19 @@ update
279 -> Key s k
302 -> Key s k
280 -> Map s k a
303 -> Map s k a
281 -> SomeMapWith (SupersetProof 'Regular s) k a
304 -> SomeMapWith (SupersetProof 'Regular s) k a
305+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
306+
307+-- | If a key is present in the map, update its value or delete it using the
308+-- given function with a proof that the key was in the map, returning a
309+-- potentially smaller map.
310+updateWithKey
311+ :: forall s k a. Ord k
312+ => (Key s k -> a -> Maybe a)
313+ -> k
314+ -> Map s k a
315+ -> SomeMapWith (SupersetProof 'Regular s) k a
282−update f k (Map m) = SomeMapWith (Map $ Map.update f (unrefine k) m)
316+updateWithKey f k (Map m) = SomeMapWith
317+ (Map $ Map.updateWithKey (f . unsafeKey) k m)
283 $ SupersetProof unsafeSubset
318 $ SupersetProof unsafeSubset
284
319
285-- | If the given key is in the map, update or delete it using the given
320-- | If the given key is in the map, update or delete it using the given
src/Data/Map/Strict/Refined.hs+37−2
@@ -57,9 +57,12 @@ module Data.Map.Strict.Refined
57 , insertLookupWithKey
57 , insertLookupWithKey
58 -- * Deletion/Update
58 -- * Deletion/Update
59 , Common.delete
59 , Common.delete
60+ , adjust'
60 , adjust
61 , adjust
61 , adjustWithKey
62 , adjustWithKey
63+ , update'
62 , update
64 , update
65+ , updateWithKey
63 , updateLookupWithKey
66 , updateLookupWithKey
64 -- * Query
67 -- * Query
65 , Common.lookup
68 , Common.lookup
@@ -245,6 +248,10 @@ insertWithKey f k v (Map m) = SomeMapWith
245
248
246-- | Overwrite a key-value pair that is known to already be in the map. The set
249-- | Overwrite a key-value pair that is known to already be in the map. The set
247-- of keys remains the same.
250-- of keys remains the same.
251+--
252+-- @
253+-- 'reinsert' k v = 'adjust (const v) k'
254+-- @
248reinsert
255reinsert
249 :: forall s k a. Ord k
256 :: forall s k a. Ord k
250 => Key s k -> a -> Map s k a -> Map s k a
257 => Key s k -> a -> Map s k a -> Map s k a
@@ -265,10 +272,15 @@ insertLookupWithKey f k v (Map m)
265 (v', !m') -> ((unsafeKey k,) <$> v',)
272 (v', !m') -> ((unsafeKey k,) <$> v',)
266 $ SomeMapWith (Map m') $ InsertProof (unsafeKey k) unsafeSubset
273 $ SomeMapWith (Map m') $ InsertProof (unsafeKey k) unsafeSubset
267
274
275+-- | If the given key is in the map, update the value at that key using the
276+-- given function. In any case, the set of keys remains the same.
277+adjust' :: forall s k a. Ord k => (a -> a) -> k -> Map s k a -> Map s k a
278+adjust' = coerce $ Map.adjust @k @a
279+
268-- | Update the value at a specific key known the be in the map using the given
280-- | Update the value at a specific key known the be in the map using the given
269-- function. The set of keys remains the same.
281-- function. The set of keys remains the same.
270adjust :: forall s k a. Ord k => (a -> a) -> Key s k -> Map s k a -> Map s k a
282adjust :: forall s k a. Ord k => (a -> a) -> Key s k -> Map s k a -> Map s k a
271−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ Map.adjust @k @a
283+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
272
284
273-- | If the given key is in the map, update the associated value using the given
285-- | If the given key is in the map, update the associated value using the given
274-- function with a proof that the key was in the map; otherwise return the map
286-- function with a proof that the key was in the map; otherwise return the map
@@ -278,6 +290,17 @@ adjustWithKey
278adjustWithKey = gcoerceWith (unsafeCastKey @s @k) $ coerce
290adjustWithKey = gcoerceWith (unsafeCastKey @s @k) $ coerce
279 $ Map.adjustWithKey @k @a
291 $ Map.adjustWithKey @k @a
280
292
293+-- | If a key is present in the map, update its value or delete it using the
294+-- given function, returning a potentially smaller map.
295+update'
296+ :: forall s k a. Ord k
297+ => (a -> Maybe a)
298+ -> k
299+ -> Map s k a
300+ -> SomeMapWith (SupersetProof 'Regular s) k a
301+update' f k (Map m) = SomeMapWith (Map $ Map.update f k m)
302+ $ SupersetProof unsafeSubset
303+
281-- | Update or delete a key known to be in the map using the given function,
304-- | Update or delete a key known to be in the map using the given function,
282-- returning a potentially smaller map.
305-- returning a potentially smaller map.
283update
306update
@@ -286,7 +309,19 @@ update
286 -> Key s k
309 -> Key s k
287 -> Map s k a
310 -> Map s k a
288 -> SomeMapWith (SupersetProof 'Regular s) k a
311 -> SomeMapWith (SupersetProof 'Regular s) k a
312+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
313+
314+-- | If a key is present in the map, update its value or delete it using the
315+-- given function with a proof that the key was in the map, returning a
316+-- potentially smaller map.
317+updateWithKey
318+ :: forall s k a. Ord k
319+ => (Key s k -> a -> Maybe a)
320+ -> k
321+ -> Map s k a
322+ -> SomeMapWith (SupersetProof 'Regular s) k a
289−update f k (Map m) = SomeMapWith (Map $ Map.update f (unrefine k) m)
323+updateWithKey f k (Map m) = SomeMapWith
324+ (Map $ Map.updateWithKey (f . unsafeKey) k m)
290 $ SupersetProof unsafeSubset
325 $ SupersetProof unsafeSubset
291
326
292-- | If the given key is in the map, update or delete it using the given
327-- | If the given key is in the map, update or delete it using the given