Function to add to Data.List
Henning Thielemann
schlepptop at henning-thielemann.de
Sat Apr 5 22:33:34 UTC 2014
Am 06.04.2014 00:27, schrieb Niklas Haas:
> On Sat, 05 Apr 2014 17:53:04 -0400, John Wiegley <johnw at fpcomplete.com> wrote:
>> I heard a talk that mentioned this transform today at Hac NYC, and was
>> surprised it wasn't already in Data.List:
>>
>> -- | Sort a list using a key on each element. This implements the
>> -- decorate-sort-undecorate paradigm, also called a Schwarzian transform.
>> sortByKey :: Ord b => (a -> b) -> [a] -> [a]
>> sortByKey f = map snd . sortBy (comparing fst) . map (\x -> (f x, x))
>>
>> I would like to propose adding it.
>>
>> John
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://www.haskell.org/mailman/listinfo/libraries
>
> This already exists in the form of
>
> sortBy . comparing
>
> or
>
> sortBy (comparing f)
Not exactly, because sortByKey memorizes the results of 'f' and (sortBy
. comparing) recomputes 'f' all the time.
More information about the Libraries
mailing list