perl6/doc/lib/Range

doc src
(title) class Range
    class Range is Iterable does Positional { ... }

 
Ranges serve two main purposes: to generate lists of consecutive numbers or strings, and to act as a matcher to check if a number or string is within a certain range. Ranges are constructed using one of the four possible range operators, which consist of two dots, and optionally a caret which indicates that the endpoint marked with it is excluded from the range.
    1 .. 5
    1^.. 5  # start point excluded
    1 ..^5  # end point excluded
    1^..^5  # start and end point excluded

 
The caret is also a prefix operator for constructing numeric ranges starting from zero:
    ^$x     # same as 0 ..^ $x.Numeric

 
Iterating a range (or calling the list method) uses the same semantics as the ++ prefix and postfix operators, ie it calls the succ method on the start point, and then generated elements). Ranges always go from small to larger elements; if the start point is bigger than the end point, the range is considered empty.
    for 1..5 { .say }       # five iterations
    ('a' ^..^ 'f').list     # 'b', 'c', 'd', 'e'
    5 ~~ ^5;                # False
    (1.1..5).list;          # (1.1, 2.1, 3.1, 4.1)

 
Use the ... sequence operator to produce lists of elements that go from larger to smaller values, or to use offsets other than increment-by-1.

Methods

(head2) min
    method min(Range:D:)

 
Returns the start point of the range.
(head2) excludes_min
    method excludes_min(Range:D:) returns Bool:D

 
Returns True if the start point is excluded from the range, and False otherwise.
(head2) max
    method max(Range:D:)

 
Returns the end point of the range.
(head2) excludes_max
    method excludes_max(Range:D:) returns Bool:D

 
Returns True if the end point is excluded from the range, and False otherwise.
(head2) bounds
    method bounds(Range:D:) returns Positional

 
Returns a list consisting of the start and end point.
(head2) list
    method list(Range:D:) returns List:D

 
Generates the list of elements that the range represents.
(head2) flat
    method flat(Range:D:) returns List:D

 
Generates the list of elements that the range represents.

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: