=NAME Yet Another S Operator: Comparisons - Part II =VERSION Maintainer: Adriano Ferreira Date: 29 Sep 2007 Last Modified: 2 Oct 2007 Number: 6 Version: 3 Status: Draft =head1 Body In the L, we've seen some of the usual relational operators in S and their enhanced syntax through chaining (which allows expressions like C<>). Another kind of comparison operators are those that, instead of true/false returns, identify the relative order between its operands: before, equal, or after. For numbers, there is the I (just like in S): 0 <=> 1 # returns that 0 is before 1 0 <=> 0 # returns that 0 equals 0 1 <=> 0 # returns that 1 is after 0 But there is a change: instead of C<-1>, C<0>, C<1>, the return is C, C, C, the values of an enumeration, which in numeric context reduces into C<-1>, C<0>, C<1>. For strings, the sort comparator was renamed. After the triad C, C, and C, it is now called the B operator. 'a' leg 'b' # Order::Increase 'a' leg 'a' # Order::Same 'b' leg 'a' # Order::Decrease That was done on purpose, to leave the S string sort comparator C free to be a generic sort comparison, in the same way that C and C were conceived. $dt_today cmp $dt_tomorrow # Order::Increase $dt_today cmp $dt_today # Order::Same $dt_today cmp $dt_yesterday # Order::Decrease The C operator is the operator of choice when comparing B items: numbers, strings, and objects. With respect to numbers and strings, its default is to act just like C<'<=>'> and C, respectively. In turn, objects may have their comparison behavior customized by the author of the code. Chaining does not make sense to these operators. But for ordering operations like sorting, finding the minimum and maximum, they are just perfect. Some examples of sorting expressions follow. (Notice that these could be made shorter using the new signatures of the C method and other S novelties.) =begin code @values.sort; # use default 'cmp' @values.sort: { $^b cmp $^a }; # reverse order @values.sort: { $^a leg $^b || $^a <=> $^b }; # a sort based on values keyed by 'name' and 'id' # respectively =end code $Revision: 73 $ =begin comment Keywords: Perl Perl 6 operators comparisons sort comparisons =end comment