[Haskell-cafe] A small oversight

Andrew Coppin andrewcoppin at btinternet.com
Sat Feb 20 07:45:21 EST 2010


Ben Millwood wrote:
> I can't answer your question (about getting minBy into the libraries)
> but I thought I'd point out some tricks:
>
> On Sat, Feb 20, 2010 at 10:47 AM, Andrew Coppin
> <andrewcoppin at btinternet.com> wrote:
>   
>> Also, constructions like
>>
>>  sortBy (compare `on` foo)
>>
>> must surely be very common.
>>     
>
> Common enough that Data.Ord introduces comparing:
>
> comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering
> comparing = (compare `on`)
>   

Heh. I didn't even notice that Data.Ord existed...

> But it would still be useful to have sortOn et al to capture the
> common technique when your sorting property is potentially expensive
> (sortOn length, for example):
>
> sortOn f = map fst . sortBy (comparing snd) . map (\x -> (x, f x))
>
> a technique which I believe is called a Schwarzian transform.
>   

Yes, that looks quite useful...

> swap_ord (compare x y) = compare y x, so usually flip compare fills
> this requirement :)
>   

*facepalm*

Damnit, why didn't *I* think of that??



More information about the Haskell-Cafe mailing list