@@ -234,8 +234,8 @@ adjust f k0 m0 = go h0 k0 0 m0
234
234
| otherwise = t
235
235
{-# INLINABLE adjust #-}
236
236
237
- -- | /O(log n)/ The expression (@'update' f k map@) updates the value @x@ at @k@,
238
- -- (if it is in the map). If (f k x) is @'Nothing', the element is deleted.
237
+ -- | /O(log n)/ The expression (@'update' f k map@) updates the value @x@ at @k@,
238
+ -- (if it is in the map). If (f k x) is @'Nothing', the element is deleted.
239
239
-- If it is (@'Just' y), the key k is bound to the new value y.
240
240
update :: (Eq k , Hashable k ) => (a -> Maybe a ) -> k -> HashMap k a -> HashMap k a
241
241
update f = alter (>>= f)
@@ -429,7 +429,18 @@ fromList = L.foldl' (\ m (k, !v) -> HM.unsafeInsert k v m) empty
429
429
{-# INLINABLE fromList #-}
430
430
431
431
-- | /O(n*log n)/ Construct a map from a list of elements. Uses
432
- -- the provided function to merge duplicate entries.
432
+ -- the provided function f to merge duplicate entries (f newVal oldVal).
433
+ --
434
+ -- For example:
435
+ --
436
+ -- > fromListWith (+) [ (x, 1) | x <- xs ]
437
+ --
438
+ -- will create a map with number of occurrences of each element in xs.
439
+ --
440
+ -- > fromListWith (++) [ (k, [v]) | (k, v) <- xs ]
441
+ --
442
+ -- will group all values by their keys in a list 'xs :: [(k, v)]' and
443
+ -- return a 'HashMap k [v]'.
433
444
fromListWith :: (Eq k , Hashable k ) => (v -> v -> v ) -> [(k , v )] -> HashMap k v
434
445
fromListWith f = L. foldl' (\ m (k, v) -> unsafeInsertWith f k v m) empty
435
446
{-# INLINE fromListWith #-}
0 commit comments