Data.Map: Add a strict version of fromListWith and fromListWithKey
Add to Data.Map two new functions fromListWith' and fromListWithKey' with strict evaluation of the combining function.
Example use: computing the histogram of a list of values:
hist xs = toList $ fromListWith' (+) (zip xs [1,1..])
With the standard fromListWith there will be a stack overflow.
How to implement it:
fromListWithKey' f xs = foldl' ins Map.empty xs
where
ins t (k,x) = Map.insertWithKey' f k x t
fromListWith' f xs = fromListWithKey' (\_ x y -> f x y) xs
Trac metadata
| Trac field | Value |
|---|---|
| Version | 7.0.3 |
| Type | FeatureRequest |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | libraries (other) |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture |