Skip to content

Commit a868564

Browse files
author
Kubo Kovac
committed
improve the documentation of fromListWith
1 parent bb4fc20 commit a868564

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Data/HashMap/Strict.hs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ adjust f k0 m0 = go h0 k0 0 m0
234234
| otherwise = t
235235
{-# INLINABLE adjust #-}
236236

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.
239239
-- If it is (@'Just' y), the key k is bound to the new value y.
240240
update :: (Eq k, Hashable k) => (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
241241
update f = alter (>>= f)
@@ -429,7 +429,18 @@ fromList = L.foldl' (\ m (k, !v) -> HM.unsafeInsert k v m) empty
429429
{-# INLINABLE fromList #-}
430430

431431
-- | /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]'.
433444
fromListWith :: (Eq k, Hashable k) => (v -> v -> v) -> [(k, v)] -> HashMap k v
434445
fromListWith f = L.foldl' (\ m (k, v) -> unsafeInsertWith f k v m) empty
435446
{-# INLINE fromListWith #-}

0 commit comments

Comments
 (0)