Skip to content

Commit

Permalink
Give all allomorphs a single cmp candidate
Browse files Browse the repository at this point in the history
- move the Rational/Rational candidate inside the Real/Real candidate
- this results in one "is default" less and one "is default" more :-)
- but 15 fewer infix:<cmp> candidates
  • Loading branch information
lizmat committed Aug 26, 2020
1 parent 39bc2bd commit 1b16da1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
16 changes: 10 additions & 6 deletions src/core.c/Order.pm6
Expand Up @@ -28,12 +28,16 @@ multi sub infix:<cmp>(\a, Real:D \b) {
}

multi sub infix:<cmp>(Real:D \a, Real:D \b) {
(nqp::istype(a, Rational) && nqp::isfalse(a.denominator))
|| (nqp::istype(b, Rational) && nqp::isfalse(b.denominator))
?? a.Bridge cmp b.Bridge
!! a === -Inf || b === Inf
?? Less
!! a === Inf || b === -Inf
nqp::istype(a,Rational) && nqp::istype(b,Rational)
?? a.isNaN || b.isNaN
?? a.Num cmp b.Num
!! a <=> b
!! (nqp::istype(a, Rational) && nqp::isfalse(a.denominator))
|| (nqp::istype(b, Rational) && nqp::isfalse(b.denominator))
?? a.Bridge cmp b.Bridge
!! a === -Inf || b === Inf
?? Less
!! a === Inf || b === -Inf
?? More
!! a.Bridge cmp b.Bridge
}
Expand Down
24 changes: 3 additions & 21 deletions src/core.c/allomorphs.pm6
Expand Up @@ -146,27 +146,9 @@ my class ComplexStr is Allomorph is Complex {
}
}

# we define cmp ops for these allomorphic types as numeric first, then Str. If
# you want just one half of the cmp, you'll need to coerce the args
multi sub infix:<cmp>(IntStr:D $a, IntStr:D $b) { $a.Int cmp $b.Int || $a.Str cmp $b.Str }
multi sub infix:<cmp>(IntStr:D $a, RatStr:D $b) { $a.Int cmp $b.Rat || $a.Str cmp $b.Str }
multi sub infix:<cmp>(IntStr:D $a, NumStr:D $b) { $a.Int cmp $b.Num || $a.Str cmp $b.Str }
multi sub infix:<cmp>(IntStr:D $a, ComplexStr:D $b) { $a.Int cmp $b.Complex || $a.Str cmp $b.Str }

multi sub infix:<cmp>(RatStr:D $a, IntStr:D $b) { $a.Rat cmp $b.Int || $a.Str cmp $b.Str }
multi sub infix:<cmp>(RatStr:D $a, RatStr:D $b) { $a.Rat cmp $b.Rat || $a.Str cmp $b.Str }
multi sub infix:<cmp>(RatStr:D $a, NumStr:D $b) { $a.Rat cmp $b.Num || $a.Str cmp $b.Str }
multi sub infix:<cmp>(RatStr:D $a, ComplexStr:D $b) { $a.Rat cmp $b.Complex || $a.Str cmp $b.Str }

multi sub infix:<cmp>(NumStr:D $a, IntStr:D $b) { $a.Num cmp $b.Int || $a.Str cmp $b.Str }
multi sub infix:<cmp>(NumStr:D $a, RatStr:D $b) { $a.Num cmp $b.Rat || $a.Str cmp $b.Str }
multi sub infix:<cmp>(NumStr:D $a, NumStr:D $b) { $a.Num cmp $b.Num || $a.Str cmp $b.Str }
multi sub infix:<cmp>(NumStr:D $a, ComplexStr:D $b) { $a.Num cmp $b.Complex || $a.Str cmp $b.Str }

multi sub infix:<cmp>(ComplexStr:D $a, IntStr:D $b) { $a.Complex cmp $b.Int || $a.Str cmp $b.Str }
multi sub infix:<cmp>(ComplexStr:D $a, RatStr:D $b) { $a.Complex cmp $b.Rat || $a.Str cmp $b.Str }
multi sub infix:<cmp>(ComplexStr:D $a, NumStr:D $b) { $a.Complex cmp $b.Num || $a.Str cmp $b.Str }
multi sub infix:<cmp>(ComplexStr:D $a, ComplexStr:D $b) { $a.Complex cmp $b.Complex || $a.Str cmp $b.Str }
multi sub infix:<cmp>(Allomorph:D $a, Allomorph:D $b) is default {
$a.Numeric cmp $b.Numeric || $a.Str cmp $b.Str
}

multi sub infix:<eqv>(Allomorph:D $a, Allomorph:D $b --> Bool:D) is default {
nqp::eqaddr($a.WHAT,$b.WHAT)
Expand Down
7 changes: 0 additions & 7 deletions src/core.c/operators.pm6
Expand Up @@ -55,13 +55,6 @@ multi sub infix:<does>(Mu:U \obj, **@roles) is raw {
X::Does::TypeObject.new(type => obj).throw
}

# we need this candidate tighter than infix:<cmp>(Real:D, Real:D)
# but can't yet use `is default` at the place where that candidate
# is defined because it uses `infix:<does>`
multi sub infix:<cmp>(Rational:D \a, Rational:D \b) is default {
a.isNaN || b.isNaN ?? a.Num cmp b.Num !! a <=> b
}

proto sub infix:<but>(Mu, |) is pure {*}
multi sub infix:<but>(Mu:D \obj, Mu:U \rolish) {
my $role := rolish.HOW.archetypes.composable() ?? rolish !!
Expand Down

0 comments on commit 1b16da1

Please sign in to comment.