infix cmp

Documentation for infix cmp assembled from the following types:

language documentation Operators

From Operators

(Operators) infix cmp

multi sub infix:<cmp>(Any,       Any)
multi sub infix:<cmp>(Real:D,    Real:D)
multi sub infix:<cmp>(Str:D,     Str:D)
multi sub infix:<cmp>(Version:DVersion:D)

Generic, "smart" three-way comparator.

Compares strings with string semantics, numbers with number semantics, Pair objects first by key and then by value etc.

if $a eqv $b, then $a cmp $b always returns Order::Same.

say (=> 3cmp (=> 4);   # OUTPUT: «Less␤» 
say 4        cmp 4.0;        # OUTPUT: «Same␤» 
say 'b'      cmp 'a';        # OUTPUT: «More␤»

Strings are compared codepoint by codepoint; if leading codepoints are the same, the result of comparing the first differing codepoint is returned or the longer string if their lenghts differ.

"abcd" cmp "abcde"    # OUTPUT: «Less␤» 
"abcd " cmp "abcde"   # OUTPUT: «Less␤» 
'A' cmp ''           # OUTPUT: «Less␤»

class RatStr

From RatStr

(RatStr) infix cmp

multi sub infix:<cmp>(RatStr:D $aRatStr:D $b)

Compare two RatStr objects. The comparison is done on the Rat value first and then on the Str value. If you want to compare in a different order then you would coerce to the Rat or Str values first:

my $f = RatStr.new(42.1"smaller");
my $g = RatStr.new(43.1"larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»

class ComplexStr

From ComplexStr

(ComplexStr) infix cmp

multi sub infix:<cmp>(ComplexStr:D $aComplexStr:D $b)

Compare two ComplexStr objects. The comparison is done on the Complex value first and then on the Str value. If you want to compare in a different order then you would coerce to the Complex or Str values first:

my $f = ComplexStr.new(42+0i, "smaller");
my $g = ComplexStr.new(43+0i, "larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»

class IntStr

From IntStr

(IntStr) infix cmp

multi sub infix:<cmp>(IntStr:D $aIntStr:D $b)

Compare two IntStr objects. The comparison is done on the Int value first and then on the Str value. If you want to compare in a different order then you would coerce to an Int or Str value first:

my $f = IntStr.new(42"smaller");
my $g = IntStr.new(43"larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»

class Pair

From Pair

(Pair) infix cmp

Defined as:

multi sub infix:<cmp>(Pair:DPair:D)

The type-agnostic comparator; compares two Pairs. Compares first their key parts, and then compares the value parts if the keys are equal.

my $a = (Apple => 1);
my $b = (Apple => 2);
say $a cmp $b# OUTPUT: «Less␤»

class NumStr

From NumStr

(NumStr) infix cmp

multi sub infix:<cmp>(NumStr:D $aNumStr:D $b)

Compare two NumStr objects. The comparison is done on the Num value first and then on the Str value. If you want to compare in a different order then you would coerce to a Num or Str value first:

my $f = NumStr.new(42.1e0"smaller");
my $g = NumStr.new(43.1e0"larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»

enum Order

From Order

(Order) infix cmp

multi sub infix:<cmp>(\a, \b --> Order:D)

cmp will first try to compare operands as strings (via coercion to Stringy), and, failing that, will try to compare numerically via the <=> operator or any other type-appropriate comparison operator. See also the documentation for the cmp operator.

class List

From List

(List) infix cmp

multi sub infix:<cmp>(List @aList @b)

Evaluates Lists by comparing element @a[$i] with @b[$i] (for some Int $i, beginning at 0) and returning Order::Less, Order::Same, or Order::More depending on if and how the values differ. If the operation evaluates to Order::Same, @a[$i + 1] is compared with @b[$i + 1]. This is repeated until one is greater than the other or all elements are exhausted.

If the Lists are of different lengths, at most only $n comparisons will be made (where $n = @a.elems min @b.elems). If all of those comparisons evaluate to Order::Same, the final value is selected based upon which List is longer.

say (123cmp (123);   # OUTPUT: «Same␤» 
say (456cmp (457);   # OUTPUT: «Less␤» 
say (789cmp (788);   # OUTPUT: «More␤» 
 
say (12)    cmp (123);   # OUTPUT: «Less␤» 
say (123cmp (12);      # OUTPUT: «More␤» 
say (9).List  cmp (^10).List;  # OUTPUT: «More␤»