Notice: Stat is currently in private beta. This documentation is incomplete and subject to change.
Stat Docs
Range Values
Range values are nothing more than a representation of a sequence of integer values.
The syntax for a range value is straight forward, it's just a starting integer value, followed by
two or three dots, followed by an optional ending integer value. Here are some code examples
If a range has two dots, that means that the ending value is not included in the range.
If it has three dots, then the ending value is included in the range.
MAIN // This is a range that starts at 0 and // ends at 10, not including 10 // So... 0-9 let singleDigitIntegers = 0..10 // This is a range that starts at 10 // and ends at 99. and 99 is included let doubleDigitIntegers = 10...99 // These are all the same value. // A range that starts at 52, and goes on forever to infinity let openRange = 52.. openRange = 52... openRange = 52..infinity openRange = 52...infinity
Something to note
If a range value has no ending integer or if that integer is infinity or negative infinity, then it doesn't matter if there are 2 or 3 dots. The value is the same either way. Ranges can go backwards as well. Have a look at these code examples:
MAIN // This is a range that starts at -1 and // ends at -10, not including -10 // So... -1 through -9 let singleDigitNegativeInts = -1..-10 // This is a range that starts at -10 // and ends at -99. and -99 is included let doubleDigitNegativeInts = -10...-99 // For a range that decreases // forever use an ending integer of -infinity // This range starts at 52 and and decreases forever let openNegativeRange = 52..-infinity
A range that starts with and ends with the same number with 2 dots in between is an empty range.
And a range that starts and ends with the same number with 3 dots in between is a single integer range.
Here are some examples:
MAIN // This is an empty range let emptyRange = 1..1 // This range contains a single number: 1 let singleNumberRange = 1...1 // These are also empty ranges emptyRange = infinity..infinity emptyRange = infinity.. emptyRange = -infinity..-infinity emptyRange = -infinity.. // These are ranges that contain a single number: infinity singleNumberRange = infinity...infinity singleNumberRange = infinity... // These are ranges that contain a single number: -infinity singleNumberRange = -infinity...-infinity singleNumberRange = -infinity...
Using Variables in Range Values
You can use any variable as the start or end value in a range as long as that variable has
a data type of
int. Here are some code examples: MAIN let start = 22 let end = 56 // This is a range that starts at 22 and // ends at 56, not including 56 // So... 22 through 55 let dynamicRange = start..end // This is a range that starts at 22 and // goes on forever until infinity let openRange = start..
Something to note
You cannot use expressions in range values. Only integer literals and variable names are allowed.Iterating a Range
You can iterate over a range using a for loop. We'll cover for loops later, but we'll give you a
quick code example so that you get the idea.
MAIN // Open up a stream so that we can print out values let console = open console for let digit in 1...9 digit to: string -> console // Outputs: 123456789
Converting a Range To a List of Integers
You can convert a range into a list of integers by using the
to operator followed by a type hint of [int]? Notice that the type hint is an optional type hint. The reason for this is that a range may not be able
to be converted into a list. If the range is an open range that goes on forever, then it can't be converted to an integer list
and in this case, the result will be empty when trying to convert it. MAIN let myRange = 1..6 let myList = myRange to: [int]? // myList is now [1, 2, 3, 4, 5] myRange = 1.. myList = myRange to: [int]? // myList is now empty
Warning!
Use caution when converting a range to a list of integers. There is no limit to how many integers can be in a list. So you could potentially run out of memory if you try to convert a range that is large enough... say1..9999999999999999. That'll take up all your
memory and probably crash the program or the computer. Previous
Next