Scala Range

 321 total views,  3 views today

Welcome to Scala series. In this tutorial, you will learn about Scala Range in detail.

Scala Range is a sequence of integers or characters with a certain order or increment. It is mainly used for looping constructs.

E.g

Range is 1,2,3,4,5,6
Even integer Range is 2,4,6,8,10
Odd integer range is 1,3,5,7,9

Integer Range using “to”

Here we are trying to create a range from 1 to 10 inclusive of 10.

scala> var range = 1 to 10
range: scala.collection.immutable.Range.Inclusive = Range 1 to 10

scala> range.foreach(println)
1
2
3
4
5
6
7
8
9
10

Integer Range using “to” with an increment

In this example, we have created a range from 1 to 10 but with an increment of 2.

scala> var range = 1 to 10 by 2
range: scala.collection.immutable.Range = inexact Range 1 to 10 by 2

scala> range.foreach(println)
1
3
5
7
9

Integer Range using “until”

Here we are trying to create a range from 1 until 10 but exclusive of 10.

scala> var range = 1 until 10
range: scala.collection.immutable.Range = Range 1 until 10

scala> range.foreach(println)
1
2
3
4
5
6
7
8
9

Integer Range using “until” with an increment

Here we are trying to create a range from 1 until 10 but exclusive of 10 and increment of 2

scala> var range = 1 until 10 by 2
range: scala.collection.immutable.Range = inexact Range 1 until 10 by 2

scala> range.foreach(println)
1
3
5
7
9

Character Range using “to”

scala> var range = 'A' to 'H'
range: scala.collection.immutable.NumericRange.Inclusive[Char] = NumericRange A to H

scala> range.foreach(println)
A
B
C
D
E
F
G
H

Character Range using “to” with an increment

scala> var range = 'A' to 'H' by 3
range: scala.collection.immutable.NumericRange[Char] = NumericRange A to H by ?

scala> range.foreach(println)
A
D
G

Create an Integer range using Scala Range API

Syntax

val range = Range(start, end, incremet)
scala> val range = Range(2, 14, 4)
range: scala.collection.immutable.Range.Exclusive = Range 2 until 14 by 4

scala> range.foreach(println)
2
6
10

Create a reverse range using “to”

scala> var range = 5 to 1
range: scala.collection.immutable.Range.Inclusive = empty Range 5 to 1

scala> range.foreach(println)

This will not work because of you creating a reverse range you have to mention the increment factor also. Else it will just create an empty range.

scala> var range = 5 to 1 by -1
range: scala.collection.immutable.Range = Range 5 to 1 by -1

scala> range.foreach(println)
5
4
3
2
1

Convert a range to collections

Set

scala> var set = (1 to 10).toSet
set: scala.collection.immutable.Set[Int] = HashSet(5, 10, 1, 6, 9, 2, 7, 3, 8, 4)

List

scala> var list = (1 to 10).toList
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Array

scala> var array = (1 to 10).toArray
array: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Sequence

scala> var seq = (1 to 10).toSeq
seq: scala.collection.immutable.Range.Inclusive = Range 1 to 10

References : https://www.scala-lang.org/api/2.12.4/scala/collection/immutable/Range.html

Thanks for reading and please give a like or comment!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.