Updated April 15, 2023
Introduction to Scala Range
The range is a sequence of Integer in scala. In the range, we can define our start and endpoint. Scala Range is an ordered sequence like 4, 5, 6 this is a range in sequence. Scala range is the part of a collection only. We can use this range by some predefined functions available for it. If we want to define it we have to use three constant for it. In other words, we can say that range is an organized series of an integer. It makes our operation fast and quick. It also reduced our effort for generating integer sequences.
Syntax:
valvariable_name = Range(parm1, parm2, parm3)
In the above syntax, this param value means something we cannot pass anything here. Our generation sequence will be highly and fully depend on this only Let’s understand them one by one in the coming section.
practical declaration of range scala;
valmyrange = Range(3, 8, 1)
This means the sequence starts from 3 the first parameter here and goes up to the second parameter. Now the third parameter is the increment value, by much we want to increment the next generated value.
e.g. >> 3, 4, 5, 6, 7
This can be the expected output we can receive by passing these are values.
How does Range Function Work in Scala?
Scala Range fully depends upon the three parameters it takes in its syntax, i.e. upper limit, lower limit, and increment value. While working with the range we have to define these constants in it. Based on this constant scala will generate one ordered sequence or an organized sequence.
Let’s discuss them one by one:
- Upper Limit Parameter: This is the first parameter of range. This parameter specifies the first value of the sequence from which we want to generate our sequence.
- Lower Limit: This is the second parameter of range. This will be the last value of our sequence. That means up to this variable the sequence will be generated for us. We cannot swap this two-variable have to provide them in sequence only.
- Increment Constant: This is the last parameter that we have to mention while working with a range in scala. This parameter represents the step by which we want to generate the next value suppose we have ‘1’ specify for this parameter that means next value will be generated by the increment of 1 in each integer value.
Now have look at some extended, superclasses and some know subclasses of range;
Extended class for range in scala.
- AbstractSeq[Int]
- IndexedSeq[Int]
- CustomParallelizable[Int, ParRange]
- Serializable
Super types for range in scala;
- Serializable
- io.Serializable
- Traversable[Int]
- Traversable[Int]
- IndexedSeq[Int]
- CustomParallelizable[Int, ParRange]
- Equals
- TraversableOnce[Int]
- AnyRef
- Any
- FilterMonadic[Int, IndexedSeq[Int]]
- Parallelizable[Int, ParRange]
- HasNewBuilder[Int, scala.collection.immutable.IndexedSeq[Int]
- @scala.annotation.unchecked.uncheckedVariance]
- GenTraversable[Int]
- GenericTraversableTemplate[Int, IndexedSeq]
- TraversableLike[Int, IndexedSeq[Int]]
- GenTraversableLike[Int, IndexedSeq[Int]]
- IterableLike[Int, IndexedSeq[Int]]
- GenIterableLike[Int, IndexedSeq[Int]], AbstractTraversable[Int]
- GenTraversableOnce[Int]
- GenIterable[Int]
- SeqLike[Int, IndexedSeq[Int]]
- IndexedSeqLike[Int, IndexedSeq[Int]]
- Seq[Int], Iterable[Int]
- Immutable
- IndexedSeq[Int]
- GenSeq[Int]
- GenSeqLike[Int, IndexedSeq[Int]]
- (Int) ⇒Int, AbstractIterable[Int]
- PartialFunction[Int, Int]
- AbstractSeq[Int]
- Iterable[Int]
- Seq[Int]
Known subclasses are:
- Inclusive
Take one example to understand its working;
object Main extends App{
// Your code here!
valmyRange = Range(5, 20, 5)
println("Range obtained is :: ")
println(myRange)
}
In this example sequence will start from 5 it will grow up to 20 by the increment of 5in each integer obtained while generation. Scala range has defined some methods which are named as to, by, and until we will use them into the example section.
Examples of Scala Range
In this method, we are using until by and to a method to initialize our range also we are using the Range keyword to initialize the sequence.
Example #1
Code:
object Main extends App{
// Your code here!
val myRange1 = Range(5, 20, 5)
val myRange2 = 0 until 20 by 3
val myRange3 = 5 until 20 by 5
println("matching our two different range initialized :: ")
println(myRange1 == myRange2)
println(myRange3 == myRange1)
}
Output:
Example #2
In this method, we are using an inclusive method to get the range defined.
Code:
object Main extends App{
// Your code here!
//creating range
val myRange1 = Range(5, 20, 5)
var value = myRange1.inclusive
// printing them out.
println("Using inclusive method :: ")
println(value)
}
Output:
Example #3
In this example, we are fetching sequence value by using their index. We just need to pass the index number inside the bracket it will print out the value for us.
Code:
object Main extends App{
// Your code here!
//creating range
val myRange1 = Range(5, 20, 5)
var value = myRange1.inclusive
var val1 = myRange1(2)
// printing them out.
println("Using inclusive method :: ")
println(value)
println("index value obtained : ")
println(val1)
}
Output:
Example #4
In this example, we are fetching out the last value from the sequence by using the last method.
Code:
object Main extends App{
// Your code here!
//creating range
val myRange1 = Range(5, 20, 5)
var value = myRange1.inclusive
var val1 = myRange1(2)
var val2 = myRange1.last
// printing them out.
println("Using inclusive method :: ")
println(value)
println("index value obtained : ")
println(val1)
println("last value obtained : ")
println(val2)
}
Output:
Example #5
In this example, we are fetching the first value from the sequence by using head method.
Code:
object Main extends App{
// Your code here!
//creating range
val myRange1 = Range(5, 20, 5)
var value = myRange1.inclusive
var val1 = myRange1(2)
var val2 = myRange1.head
// printing them out.
println("Using inclusive method :: ")
println(value)
println("index value obtained : ")
println(val1)
println("first value obtained : ")
println(val2)
}
Output:
Example #6
In this example, we are using to the method of scala range.
Code:
object Main extends App{
// Your code here!
//creating range
val myRange1 = Range(5, 20, 5)
var value = 5 to 20
// printing them out.
println("index value obtained : ")
println(value == myRange1)
}
Output:
Conclusion
Scala range is used to generate some sequence of an integer. It is also part of the collection in scala and they are immutable in nature. Scala range provides us various methods to create a sequence or series of integers which makes the execution quick and fast. Basically it made up of three constant in scala.
Recommended Articles
We hope that this EDUCBA information on “Scala Range” was beneficial to you. You can view EDUCBA’s recommended articles for more information.