[Haskell-cafe] Dynamically altering sort order
Denis Bueno
dbueno at gmail.com
Fri Apr 24 17:23:54 EDT 2009
On Fri, Apr 24, 2009 at 15:22, Martijn van Steenbergen
<martijn at van.steenbergen.nl> wrote:
> Hi Denis,
>
> Denis Bueno wrote:
>>
>> where the rCompare field would be a function that is based on the
>> flags passed to the command-line problem. But this has an ugly
>> asymmetry. Does anyone have any other ideas?
>
> Here's a solution that is more symmetrical but not necessarily prettier:
>
> newtype Wrap = Wrap { unwrap :: Record }
>
> instance Ord Wrap where
> ... your compare function here ...
The problem here is that the order is fixed. Statically. I can't
change it at runtime based on flags. (Right? Unless I'm missing
something....)
> But I don't think there's anything wrong with your original solution. You
> can write your own sortBy and hide (not export) the CompareRecord type:
>
> sortBy :: (a -> a -> Ord) -> [a] -> IO [a]
> sortBy cmp = map unCR . sort . map (CompareRecord cmp)
Right, that's what I was thinking, too. The asymmetry was giving me
the willies, though. =]
Denis
More information about the Haskell-Cafe
mailing list