Scala Range Examples: Start, End and StepCreate a range with the to method. Use the by function to specify a step.
dot net perls
Range. Numbers are in a range. We have 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.
A simple range.
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.
IsInclusive This returns true. An inclusive range includes the final number (the end) of the range. To() creates inclusive ranges.
IsEmpty Returns false in this program because the range has more than zero elements. An empty range has 0 numbers.
Start, End, Step A range's start is its first number. Its end is its last. The step is the amount each number is incremented.
// Create range from 10 to 13 inclusive.
Scala program that creates range
val range = 10.to
// Print some properties of the range.
println("IsInclusive = "
println("IsEmpty = "
println("Start = "
println("End = "
println("Step = "
// Sum all elements in the range.
val total = range.sum
println("Sum = "
Range(10, 11, 12, 13)
IsInclusive = true
IsEmpty = false
Start = 10
End = 13
Step = 1
Sum = 46
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.
Tip To() creates an inclusive range (the last number is included). And "until" creates an exclusive range (the last number is omitted).
// Create a range of even numbers.
// ... Use a step of 2 with the by function.
Scala program that creates ranges with steps
val evens = 10.to
// 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
Range(10, 12, 14, 16, 18, 20)
Range(20, 15, 10, 5)
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.
// Create a range of 10 until 13 (end is exclusive).
Scala program that creates ranges
val range1 = Range
// Create a range of 10 until 20 with step of 3.
val range2 = Range
(10, 20, 3)
Range(10, 11, 12)
Range(10, 13, 16, 19)
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).
Scala program that uses contains
val testRange = 10.to(12)
// This value is contained in the range.
// This value is not present.
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.
© 2007-2021 sam allen. send bug reports to email@example.com.