updateWithKey

updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
containers Data.IntMap.Strict, containers Data.IntMap.Lazy
O(min(n,W)). The expression (update f k map) updates the value x at k (if it is in the map). If (f k x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y. > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
containers Data.Map.Lazy, containers Data.Map.Strict
O(log n). The expression (updateWithKey f k map) updates the value x at k (if it is in the map). If (f k x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y. > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"