Be advised that I have been learning Gleam for fun all of maybe three afternoons, but I do have a way you can kind of do this: You can define your own type that is an Int that can also be Inf (or MinInf). I wouldn't recommend it much probably, but some algorithms do look a lot nicer if you can just use an Int value that you can be certain will always be larger than any normal Int, so this does that. Only problem is, you're then using your own type instead of the built-in Int, so the normal operators don't work anymore, and you can't overload them either. (I'm sure there's a reason for that, but I'd love to be able to myself at the moment.) Anyway here's the code:
import gleam/io
pub type IntOrInf{
Integer(Int)
Inf
MinInf
}
pub fn is_greater_or_equal(a: IntOrInf, compared_to b: IntOrInf) -> Bool {
case a, b {
Integer(a), Integer(b) -> a >= b
Integer(_), Inf -> False
Inf, Integer(_) -> True
Integer(_), MinInf -> True
MinInf, Integer(_) -> False
Inf, MinInf -> True
MinInf, Inf -> False
Inf, Inf -> True
MinInf, MinInf -> True
}
}
pub fn is_greater(a: IntOrInf, than b: IntOrInf) -> Bool {
is_greater_or_equal(a, b) && a != b
}
pub fn is_less(a: IntOrInf, than b: IntOrInf) -> Bool {
!is_greater_or_equal(a, b)
}
pub fn is_less_or_equal(a: IntOrInf, compared_to b: IntOrInf) -> Bool {
is_less(a, b) || a == b
}
pub fn main() {
io.debug(is_less(Integer(5), than: Inf))
io.debug(is_less(Integer(5), than: MinInf))
io.debug(is_greater(Inf, MinInf))
io.debug(is_greater(Inf, than: Inf))
io.debug(is_greater_or_equal(Inf, compared_to: Inf))
io.debug(Inf == Inf)
// et cetera...
}
I haven't tested all of the cases nor put much though into it, so it might not be 100% correct, but you get the idea, I hope.
Edit: Maybe it'd be better to define a function that works like int.compare, which returns an object of type Order, which can have the values (variants?) Eq, Gt or Lt. That way you can use the IntOrInf in the standard list.sort.
Edit 2: The comparison function can be written more compactly like below. Note that now the order of the cases is important, because we want to get True when comparing Inf to Inf or MinInf to MinInf.
pub fn is_greater_or_equal(a: IntOrInf, compared_to b: IntOrInf) -> Bool {
case a, b {
Integer(a), Integer(b) -> a >= b
Inf, _ -> True
_, Inf -> False
_, MinInf -> True
MinInf, _ -> False
}
}