Proposal #2659: Add sortOn and friends to Data.List

Jean-Philippe Bernardy bernardy at chalmers.se
Mon Oct 6 17:32:54 EDT 2008


How about capturing the pattern in higher order functions?

if we define:

> import Data.Function
> import Data.List

> data Cache b a = Cache b a
> fromCache (Cache _ a) = a
> toCache f a = Cache (f a) a
> cache (Cache b _) = b

> caching f g = map fromCache . g . map (toCache f)

then sortOn becomes:

> sortOn2 f = caching f (sortBy (compare `on` cache))

and with the instances

> instance Ord b => Ord (Cache b a) where
>    compare x y = compare (cache x) (cache y)
>
> instance Eq b => Eq (Cache b a) where
>    x == y = cache x == cache y

it gets even simpler:

> sortOn3 f = caching f sort

It could be that I missed the point, but I'd much rather not see so
many functions
added to base without a strong case for it.

Cheers,
JP.


More information about the Libraries mailing list