Open Andriamanitra opened 6 months ago
I think Array.quicksortPartition has the bug. in the case that anything is less than the pivot it adds one, but that is a bug if pivot is less than itself
The definition of Order
is not reflexive. Can you try with an updated version of Order
?
This seems to work:
enum Hand with ToString, Eq {
case FiveOfAKind(String)
case FourOfAKind(String)
case FullHouse(String)
case ThreeOfAKind(String)
case TwoPair(String)
case OnePair(String)
case HighCard(String)
}
instance Order[Hand] {
pub def compare(x: Hand, y: Hand): Comparison =
def toN(h) = match h {
case Hand.FiveOfAKind(_elem) => 1
case Hand.FourOfAKind(_elem) => 2
case Hand.FullHouse(_elem) => 3
case Hand.ThreeOfAKind(_elem) => 4
case Hand.TwoPair(_elem) => 5
case Hand.OnePair(_elem) => 6
case Hand.HighCard(_elem) => 7
};
toN(x) <=> toN(y)
}
def main(): Unit \ IO =
let testHands = Hand.FiveOfAKind("JJJJJ") :: Hand.HighCard("29QA4") :: Hand.TwoPair("6A9A9") :: Nil;
testHands |> List.sort |> ToString.toString |> println
I think the comment is that even if order is wrong, you shouldn't crash the program.
It works once the definition is reflexive, but the point is that it shouldn't crash at runtime even when it isn't. A compile time crash / error would be fine I guess but I don't think compilers can prove whether the definition is reflexive or not.
I ran into this issue while solving advent of code 2023 day 7 using Flix version 0.42.0. I was still working on the code so the
Order
was not well defined, but I would still expect it to run and return some kind of list (even if it's in a weird order) instead of running into index out of bounds exception.Here's a sort of minimized reproduction:
It crashes with the following error: