holgersiegel74 at yahoo.de
Sun Dec 7 09:01:10 EST 2008
On Saturday 06 December 2008 22:07:51 Paul Johnson wrote:
> So we could have
> fromListWithZero :: Ord k => (a -> b -> b) -> b -> [(k, a)] -> Map k b
> fromListWithZero combiner zero pairs = ...
> The first time a key is seen the combining function is called with
> "zero" as its second argument. E.g.
> fromListWithZero (:)  xs
> Or is that too much trouble?
It could be made a bit more general and efficient: Every entry x that did not
have to be combined with another will end up as an application (combiner zero
x). Thus, you could also write a function
fromListWithUnit :: Ord k => (a -> b -> b) -> (a -> b) -> [(k, a)] -> Map k b
fromListWithUnit combiner unit pairs = ...
and define fromListWithZero via
fromListWithZero c z ps = fromListWithUnit c (c z) ps
But there is
fromListWithUnit c u = fromListWith c . map (\p ->(fst p, u (snd p)))
So, you already have what you want.
More information about the Haskell-Cafe