Proposal: add an order-reversing newtype to Data.Ord

Twan van Laarhoven twanvl at gmail.com
Thu Jun 28 16:40:52 CEST 2012


On 2012-06-28 12:01, ARJANEN Loïc Jean David wrote:
> Hello all,
>
> I recently noticed that despite conversations about that in this
> mailing-list, there isn't an order-reversing newtype in Data.Ord.
> That newtype would be quite useful, e.g. for reverse-sorting a list or
> a set, so I propose we add a newtype defined thusly:
>
>     newtype Reverse a = Reverse { fromReverse :: a } deriving (Eq)
>
>     instance Ord a => Ord (Reverse a) where
>        compare (Reverse x) (Reverse y) = compare y x
>
> I took the implementation from the Down newtype in GHC.Exts, so we
> could perhaps just re-export it.
> This will cause breakage to code already defining a type named
> Reverse, so I don't know if the damage will be important. Of course,
> the name and definition are open to discussion.

+1 with any sensible name except Dual.

The name Dual is also used for dual monoids. I would prefer to be able to tell 
from a piece of code like `f (Dual x)` whether it reverses the order of the 
arguments to `mappend` or of the arguments to `compare`.

As a good compromise, we could call the newtype "DualOrd" or something.

The extraction function should maybe be called getReverse (or getWhatever), 
which is the convention used by the wrappers in Data.Monoid.


Twan



More information about the Libraries mailing list