
6 Oct
2008
6 Oct
'08
9:32 p.m.
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.