Home
Map
Range ExamplesCreate a range with the to method. Use the by function to specify a step.
Scala
This page was last reviewed on Dec 13, 2023.
Range. Think of some numbers in a range—10, 11, 12. These start at 10, end at 12, and have a step of 1. In Scala these numbers can be specified with built-in functions.
By collecting numbers together, we can use functions (like sum) that act on a group of numbers. This is a functional programming approach. It may lead to clearer code.
This example first creates a range from 10 to 13 inclusive with the to function. It prints the range. Then it uses some properties of the range.
Note IsInclusive returns true. An inclusive range includes the final number (the end) of the range. To() creates inclusive ranges.
Next IsEmpty returns false in this program because the range has more than zero elements. An empty range has 0 numbers.
Info A range's start is its first number. Its end is its last. The step is the amount each number is incremented.
Int
object Program { def main(args: Array[String]): Unit = { // Create range from 10 to 13 inclusive. val range = 10.to(13) println(range) // Print some properties of the range. println("IsInclusive = " + range.isInclusive) println("IsEmpty = " + range.isEmpty) println("Start = " + range.start) println("End = " + range.end) println("Step = " + range.step) // Sum all elements in the range. val total = range.sum println("Sum = " + total) } }
Range 10 to 13 IsInclusive = true IsEmpty = false Start = 10 End = 13 Step = 1 Sum = 46
Steps. Let us continue with ranges. This program uses to() and until to build ranges. But then it adds another feature, the by() function, to specify a step on these ranges.
println
Tip To() creates an inclusive range (the last number is included). And "until" creates an exclusive range (the last number is omitted).
object Program { def main(args: Array[String]): Unit = { // Create a range of even numbers. // ... Use a step of 2 with the by function. val evens = 10.to(20).by(2) println(evens) // Use until to create a range that omits the final number (exclusive end). // ... Use by to specify a negative step. // ... The range is descending from high to low. val descending = 20.until(0).by(-5) println(descending) } }
Range 10 to 20 by 2 Range 20 until 0 by -5
Create range. A range can be created directly with the Range type. This is sometimes a clearer way to create a range. With this syntax, the "end" is exclusive (it is not part of the range).
Tip This syntax form has the same effect as the "until" and "by" functions on Ints.
object Program { def main(args: Array[String]): Unit = { // Create a range of 10 until 13 (end is exclusive). val range1 = Range(10, 13) println(range1) // Create a range of 10 until 20 with step of 3. val range2 = Range(10, 20, 3) println(range2) } }
Range 10 until 13 inexact Range 10 until 20 by 3
Contains. Is a value present in a range? With the contains() function, we determine if the argument exists in a range. It returns true or false (a Boolean).
object Program { def main(args: Array[String]): Unit = { val testRange = 10.to(12) // This value is contained in the range. if (testRange.contains(10)) { println(true) } // This value is not present. if (!testRange.contains(14)) { println(false) } } }
true false
Ranges are helpful. They describe series of numbers. This makes calling functions on sequences easier and more intuitive. With methods like to, until and by we form simple ranges.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 13, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.