Containers and folds

Thomas Schilling nominolo at googlemail.com
Fri Sep 24 13:45:14 EDT 2010


On 24 September 2010 18:09, Milan Straka <fox at ucw.cz> wrote:
> Hi,
>
> the Set, and IntSet have one 'fold' that does not specify order of
> folding (but in fact are right folds).
>
> Map and IntMap have 'fold' and 'foldWithKey' which is a right fold
> (and documented as such).
>
> Recently foldrWithKey and foldlWithKey was added to Data.Map
> (foldWithKey is deprecated and a synonymous for foldrWithKey).
>
> There are two thinks to sort out: a) add left/right folds, b) add strict
> folds.
>
> I therefore propose to:
> a) add foldrWithKey and foldlWithKey to IntMap, make foldWithKey
>   deprecated and synonymous for foldrWithKey.
> b) add foldl and foldr to Set and IntSet, make fold deprecated
>   and synonymous to foldr.
> c) add strict variants of left and right folds, namely
>  Data.Map, Data.IntMap: foldlWithKey' foldrWithKey'
>  Data.Set, Date.IntSet: foldl', foldr'
>
> Some issues:
> - should we really rename fold -> foldr? Maybe we could cla
>  that fold and foldWithKey are right folds and add only
>  foldl and foldlWithKey.
> - should we really deprecate fold and foldWithKey? It could break
>  a lot of code.
> - adding foldl, foldr, foldl' and foldr' to Set and IntSet is going to
>  cause a lot of trouble because of shadowing. Maybe some better name?
>  On the other hand, these are the "right" names.
>
> Discussion period: three weeks (because of ICFP).
> Discussion ends: 15th Oct.

Since you say this is no formal proposal, but simply a request for
comments, you don't need a deadline.

Regarding the questions:  I prefer the foldr/l names, because
otherwise I may need to look up the documentation to figure out which
fold it is. It's much better to use the standard naming convention
from lists.  As an example, there is foldM which is actually a left
fold.  Data.Foldable fixes this and only uses the generic name 'fold'
for the Monoid case where order does indeed not matter due to
associativity of mappend.

Yes, foldr/l clashes with the default names, but Data.[Int]Map/Set
already export clashing names and therefore need to be used with
qualified exports.  Therefore I see no reason why we would need to
create special fold names.

Deprecating the existing names is a problem, yes, but we need numbers
to see how many packages are actually affected.  Also, what's the
usual timeframe between deprecation and removal?  Two years?

/ Thomas

-- 
Push the envelope. Watch it bend.


More information about the Libraries mailing list