Map / Set folding API changes

Scott Dillard sedillard at
Thu Oct 2 13:28:10 EDT 2008

Regarding this thread :

and this ticket :

I've attached a patch (to trac) which expands on Evan's changes and
propagates them to IntSet and IntMap.

Some (possibly controversial) changes of note :

    * Added foldr and foldl functions for the map types. This is
redundant, but so is much of the interface. The argument to
foldlWithKey has a strange type, (a -> key -> value -> a), that does
not place nice with flip and const. So I think its convenient to have
foldl as well, and since we've got that why not throw foldr in too.

    * Specialized default implementations of foldl and foldr for
Foldable class. This is perhaps better than exporting then from the
map libraries. Data.Sequence does it this way, but the map libraries
do not. If the API clutter is found to be unbearable then perhaps a
major cleanup is in order, hiding all folds in the map/set modules and
exporting them via Foldable.

Questions remaining :

Strict versions? I'm happy to add them. The only question is if it
(further) clutters the API.

As mentioned, the type of foldlWithKey is strange. It didn't dawn on
me until I went to define foldl in terms of foldlWithKey. Perhaps
change it to (a -> (k,v) -> a) ? This goes against the established
(and IMO unfortunate) convention that all the "WithKey" functions pass
the key as an additional argument, instead of tupling it with the


More information about the Libraries mailing list