What is Range Function in Scala?
In Scala, we used to define a range function as a sequence of numbers that would increase the efficiency of the code. You can specify the starting and ending elements to the entire list, and they will be populated automatically. You can also specify the strep size by which the values increment.
Composition of the Range function in Scala in Ubuntu 20.04:
The general syntax of the Range function we used in Scala is given below:
Where “i” is the lower limit, “j” is the upper limit and “k” is an optional parameter for increment values. We have defined these constants in the Scala range function on behalf of these constants. These constants will generate the ordered and organized sequence.
How to utilize Range function in Scala in Ubuntu 20.04:
To understand the basics of using the Range function in Scala, we should take a look at the examples:
Example # 1: Using “in” Range function in Scala:
The Scala range function allows us to loop over integers directly. The initial argument is the preliminary element and the second class is the exclusive end. The third is the optional argument that is the sequence step size. By which the range sequence is incremented.
The class name is declared as “RangeExample” in this example code. The class “RangeExample” defines the main function that shows how the Scala Range Function works. Here, we have a variable defined as “R1” which initializes the sequence by using the Range function. In the range function, we have set the Lower limit by 2, the upper limit value by 9, and the increment value by 1. The print function displays the given range sequence. The other print functions will fetch out the index values and the last value from the sequence using the last method.
We are displaying the output value obtained from the above code.
Example # 2: Using Range function in Scala with “to” Method
Creating the Range sequence by using the “to” keyword. The “to” keyword is a method that creates range inclusive. It will incrementally add from the start till the last number. We have an example code below of this method.
Initially, we have to form an object of Class “XYZ” and declare the main function. The Main function will initialize the Scala Range Function within a variable. The first variable is represented with the keyword “val” and defined with the name “X1”. In variable “X1” range is given that will generate a sequence from 1 till 10. The second variable, “Y1” is specified with the upper bound of the range. We have applied the Scala range function with the keyword “to” in a variable “Z1” to get an expected sequence. At last, we have a print statement that will display the Boolean value true if the range of sequence is equal.
The output with the Boolean value is true on the console screen.
Example # 3: Using Range function in Scala with “until” Method:
We are using the “until” method in our Scala range function. It will stop one number before the final number starts.
Let’s have an example code. We have a class called “Values”. In the class “Values” main function, we have defined a variable “V1” which initializes the range sequence. That will be presented in the print statement. The other variable, “V2” has an “until” method. Until the method is exclusive of the end values. The print statement will display the expected output results and the Boolean value of the sequence until the method sequence is the same.
The desired output sequence range is shown in the image below.
Example # 4: Using Range function in Scala with “by” Method:
The example uses the “by” keyword for the Scala range function. The “by” method will perform the work of increment by the specific value assigned. You can see below code execution with the “by” Method.
Here we have a class called “Multiples”. The class “Multiples” defines the main function. In the main function, we have a variable represented as M1 in which the range function is applied. The other variable M2 initialized values with the “by” method. The sequence here increments with a value of 5; Then, we are taking a Boolean value for whether the Range sequence is equal or not.
Upon execution of this Scala script, we have got the sequence of range and the sequence of multiple 5 along with the Boolean value false. The Boolean value is false because the two sequences are not equal.
Example # 5: Using Range function in Scala for the alphabetic sequence:
We have the Range function constant on numeric values in all the above example codes. Now in this example, we have alphabets. These alphabets fetch through using Range Constant method. We have a code below for fetching alphabets.
In the code, we have declared a class “alphabets”. In the class “alphabets” main function, we have defined four variables as “a”, “b”, “c”, and “d”. These four variables are initialized with the different constants of the range function. The variable “a” uses the “until” method with the “by” method that will increment a value by “3”. The variable “b” uses the “to” method along with the “by” method. At last, the variable “c” and “d” will get the head and last values of the sequence. The print statement will show the range sequence.
The output is revealed on the terminal screen of Ubuntu 20.04 in the image below.
The article was aimed to teach the usage of the Range function in Scala using Ubuntu 20.04. Range Function in Scala is helpful. This makes calling a function on sequence easier with its constant “to”, “until”, and “by”. Through these constants, we can simply form ranges. We have different example codes of the Range function. These examples will help you a lot in dealing with range sequences in the Scala programming language.