=NAME Yet Another S Operator: Range Operators
=VERSION
Maintainer: Adriano Ferreira
Date: 17 Dec 2007
Last Modified: 18 Dec 2007
Number: 9
Version: 3
Status: Draft
=head1 Body
In S, you may construct ranges with expressions like
$min .. $max
$min ^.. $max
$min ..^ $max
$min ^..^ $max
and even
^$limit
These operators are C object constructors, specified by
their endpoints. These endpoints are excluded if there is
C<^> in the corresponding side of the range operator. Thus,
0 ..^ 4 # equivalent to 0, 1, 2, 3
1 .. 4 # equivalent to 1, 2, 3, 4
1 ^.. 3 # equivalent to 2, 3
-2 ^..^ 2 # equivalent to -1, 0, 1
Range objects are lazy iterators. So they can represent efficiently
very large or even infinite lists.
Reversed ranges cannot be constructed by just exchanging the endpoints.
So C<2..1> is always a null range. The solution is to use one of the
expressions below.
2 .. 1 :by(-1)
reverse 1 .. 2
where C has the benefit of working even for alphabetic ranges.
To build possibly unbounded ranges, C<*> may occur in any of the
endpoints.
0..* # 0 .. +Inf
'a'..* # 'a' .. 'zzzzz...'
*..0 # -Inf .. 0
1.2.3..* # Any version higher than 1.2.3
May..* # May through December
These examples also show that to build ranges it is enough
to have endpoints whose types are C, which basically
means there exist sensible comparisons among its instances.
To know more about S Ranges, their methods
and expected semantics, read
L< section "Range Semantics" from Synopsis 03 |
http://perlcabal.org/syn/S03.html#Range_semantics >.
The upto operator (unary C<'^'>)
is really a shortcut to generate a range from C<0>
up to one less than its argument.
^$limit # 0 ..^ $limit
which is handy for cases like this:
.say for ^4 # 0, 1, 2, 3
If applied to a type name, C<^Moose> is short for C,
taking from its argument to the metaclass instance instead.
This behavior is akin to "what's this thing's domain" which
correlates abstractly to how C<^$n> works.
The operator C<'..'> was relieved of the S semantics
of flip-flop operator, whose S counterpart will be seen
in yet another article of this series.
As a curiosity, notice that to construct ranges
within S regular expressions, the C<'-'>
was consistently replaced by C<'..'>.
tr/a..z/A..Z/;
$Revision: 94 $
=begin comment
Keywords:
Perl
Perl 6
operators
range operators
upto operator
=end comment