Added `foldMapWithKey`. #24

Merged
merged 1 commit into from Jul 2, 2013

Projects

None yet

2 participants

@ekmett
Member
ekmett commented Dec 25, 2012

I've needed this repeatedly over the years and it makes an asymptotic difference in parts of ekmett/lens, so I figured I'd submit it.

This is asymptotically more efficient than using fold . mapWithKey for many monoids because it doesn't have to build up an entire new map just to fold it.

There isn't a sensible strict variant.

@foxik
Member
foxik commented Dec 28, 2012

Hi,

thanks :)

I assume you mean Foldable.fold instead of Prelude.fold in the @'foldMapWithKey' f = 'Prelude.fold' . 'mapWithKey' f@ documentation comment.

I am not sure why is foldMapWithKey f = foldlWithKey (\a k b -> amappendf k b) mempty asymptotically less efficient than your definition. I realize that my and your definition differ in bracket positions (the foldlWithKey creates a linear "string" of mappends, while your definition is recursive), which could make a difference when mappend ignores an argument -- is this the key difference? If so, the properties of your definition should be mentioned in the documentation so that users can rely on this behaviour.

Nevertheless, could we move this discussion to the libraries@haskell... mailing list? Please create a proposal there, so others can be part of the discussion too.

Cheers,
Milan

@ekmett
Member
ekmett commented Dec 28, 2012

I originally just sent it here because I didn't think there was a lot to bikeshed on the issue, but the libraries@ ticket has been created.

@ekmett
Member
ekmett commented Dec 28, 2012

Consider the Monoid (First a, Last a) -- one of those is going to have to do a lot of work no matter which direction you choose between foldr and foldl. I can extract this with foldMap, but if I want the keys I have to start over, or I have to implement foldMapWithKey using traverseWithKey on Const.

@foxik foxik merged commit 0eb7a34 into haskell:master Jul 2, 2013
@foxik
Member
foxik commented Jul 2, 2013

Sorry it took such a long time, I forgot about the issue as there was no summary on libraries@...

Cheers,
Milan

@ekmett
Member
ekmett commented Jul 3, 2013

Thanks! Sorry for not following up sooner.

@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request May 18, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
116497f
@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request May 23, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
1824476
@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request May 24, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
5515c1e
@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request May 27, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
75bbac9
@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request May 30, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
1a17501
@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request Jun 2, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
41792a9
@monkey-mas monkey-mas added a commit to monkey-mas/scalaz that referenced this pull request Jun 3, 2016
@monkey-mas monkey-mas Add foldMapWithKey to IMap
Add a general foldMap function except that key is used for an arg
of a foldMap function.[1]
As mentioned by foxik[2], add a special case in foldMapWithKey when
foldMapping a leaf, which allows us not to foldMap redundantly.

[1] haskell/containers#24
[2] haskell/containers@e60634d
515047e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment