If you think about it, traverseWithKey already mimics the bracketing of IntMap internally.<br><br><div class="gmail_quote">On Sun, Dec 30, 2012 at 5:42 AM, Milan Straka <span dir="ltr">&lt;<a href="mailto:fox@ucw.cz" target="_blank">fox@ucw.cz</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<div class="im"><br>
&gt; -----Original message-----<br>
&gt; From: Edward Kmett &lt;<a href="mailto:ekmett@gmail.com">ekmett@gmail.com</a>&gt;<br>
&gt; Sent: 28 Dec 2012, 18:36<br>
&gt;<br>
&gt; I&#39;d like to add foldMapWithKey to Data.Map and Data.IntMap.<br>
&gt;<br>
&gt; Right now you can foldrWithKey and foldlWithKey on Data.Map and<br>
&gt; Data.IntMap, but you cannot foldMapWithKey.<br>
&gt;<br>
&gt; You can fake this by using fold . mapWithKey but this requires converting<br>
&gt; the entire structure.<br>
&gt;<br>
&gt; You can also implement it with traverseWithKey and the use of the Const Monoid<br>
&gt; in a manner similar to foldMapDefault.<br>
<br>
</div>One can implement foldMapWithKey as<br>
<br>
foldMapWithKey f = foldlWithKey (\a k b -&gt; a `mappend` f k b) mempty<br>
<br>
This is a valid definition which does not create intermediate structure.<br>
The difference to the proposed implementation is, as Edward mentions:<br>
<div class="im"><br>
&gt; When you have a monoid that can take advantage of the balanced nature of<br>
&gt; the tree, it&#39;d be nice to be able to not lose the original near balanced<br>
&gt; parenthesization of the source tree.<br>
<br>
</div>Both definitions differ in the bracketing of the mappend-s.<br>
This should be mentioned in the documentation of the foldMapWithKey.<br>
However, while the bracketing of foldlWithKey and foldrWithKey is well<br>
defined, the bracketing of foldMapWithKey depends on the internal<br>
structure of the container. But it is probably safe to assume that the bracketing<br>
will be &quot;balanced&quot; in some way for any implementation of the data structure.<br>
<br>
Cheers,<br>
Milan<br>
</blockquote></div><br>