comparison src/core.c/Order.pm6 @ 0:c341f82e7ad7 default tip

Rakudo branch in cr.ie.u-ryukyu.ac.jp
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 26 Dec 2019 16:50:27 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c341f82e7ad7
1 ## Order enumeration, for cmp and <=>
2 my enum Order (:Less(-1), :Same(0), :More(1));
3 role Rational { ... }
4
5 sub ORDER(int $i --> Order) {
6 nqp::if($i,nqp::if(nqp::islt_i($i,0),Less,More),Same)
7 }
8
9 proto sub infix:<cmp>($, $, *%) is pure {*}
10 multi sub infix:<cmp>(\a, \b) {
11 nqp::eqaddr(nqp::decont(a), nqp::decont(b))
12 ?? Same
13 !! a.Stringy cmp b.Stringy
14 }
15 multi sub infix:<cmp>(Real:D \a, \b) {
16 a === -Inf
17 ?? Less
18 !! a === Inf
19 ?? More
20 !! a.Stringy cmp b.Stringy
21 }
22 multi sub infix:<cmp>(\a, Real:D \b) {
23 b === Inf
24 ?? Less
25 !! b === -Inf
26 ?? More
27 !! a.Stringy cmp b.Stringy
28 }
29
30 multi sub infix:<cmp>(Real:D \a, Real:D \b) {
31 (nqp::istype(a, Rational) && nqp::isfalse(a.denominator))
32 || (nqp::istype(b, Rational) && nqp::isfalse(b.denominator))
33 ?? a.Bridge cmp b.Bridge
34 !! a === -Inf || b === Inf
35 ?? Less
36 !! a === Inf || b === -Inf
37 ?? More
38 !! a.Bridge cmp b.Bridge
39 }
40 multi sub infix:<cmp>(Int:D \a, Rational:D \b) {
41 a.isNaN || b.isNaN ?? a.Num cmp b.Num !! a <=> b
42 }
43 multi sub infix:<cmp>(Rational:D \a, Int:D \b) {
44 a.isNaN || b.isNaN ?? a.Num cmp b.Num !! a <=> b
45 }
46 multi sub infix:<cmp>(Int:D \a, Int:D \b) {
47 ORDER(nqp::cmp_I(nqp::decont(a), nqp::decont(b)))
48 }
49 multi sub infix:<cmp>(int $a, int $b) {
50 ORDER(nqp::cmp_i($a, $b))
51 }
52
53 multi sub infix:«<=>»(Int:D \a, Int:D \b) {
54 ORDER(nqp::cmp_I(nqp::decont(a), nqp::decont(b)))
55 }
56 multi sub infix:«<=>»(int $a, int $b) {
57 ORDER(nqp::cmp_i($a, $b))
58 }
59
60 # vim: ft=perl6 expandtab sw=4