Scala - NumericRange



You can create and manipulate sequences of numbers using NumericRange in Scala. NumericRange is a range of numbers with a given step value between elements. So, it is easy to work with sequences of numbers in a flexible manner.

Declaring NumericRange Variables

The following is the syntax for declaring a NumericRange variable -

Syntax

val range: NumericRange.Inclusive[Int] = 1 to 10

Here, range is declared as an inclusive NumericRange of integers from 1 to 10. NumericRanges provide various methods to generate and manipulate sequences of numbers.

You can create NumericRanges using methods like to, until, and by. These methods generate ranges with specific start, end, and step values.

Creating Inclusive Range

You can create an inclusive range that includes both the start and end values.

Example

Try following example for creating inclusive range from 1 to 10 -

import scala.collection.immutable.NumericRange

object Demo {
   def main(args: Array[String]): Unit = {
   
      // Inclusive range from 1 to 10
      val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
   
      // Elements of the range
      println(range.mkString(", "))
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

You can create NumericRanges using methods like to, until, and by. These methods generate ranges with specific start, end, and step values.

Creating Exclusive Range

You can create an exclusive range that excludes the end value using the until method.

Example

Try following example for creating exclusive range from 1 to 10 -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Exclusive range from 1 to 10
    val range: NumericRange.Exclusive[Int] = NumericRange(1, 10, 1)
    // Elements of the range
    println(range.mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

1, 2, 3, 4, 5, 6, 7, 8, 9

Creating Range with Step Value

You can create range with given step value using the by method. You can specify the increment between elements in the range.

Example

Try following example for creating range with given step value 2 -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1 to 10 with step value 2
    val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 2)
    // Elements of the range
    println(range.mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

1, 3, 5, 7, 9

Filtering NumericRanges

You can filter elements in a NumericRange using the filter() method. This method returns a new range containing only the elements that satisfy the predicate.

Example

Try following example for filtering only even numbers from given range 1 to 10 -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1 to 10
    val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
    // Filtering only even numbers from the range
    val evenRange = range.filter(_ % 2 == 0)
    // Filtered elements of given range
    println(evenRange.mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

2, 4, 6, 8, 10

Transforming NumericRanges

You can transform a NumericRange by applying a transformation function to each element using the map() method. This method returns a new range with the transformed elements.

Example

Try following example for squaring elements of given range from 1 to 10 -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1 to 10
    val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
    // Squaring each element using map function
    val squaredRange = range.map(x => x * x)
    // Squared elements of given range
    println(squaredRange.mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

1, 4, 9, 16, 25, 36, 49, 64, 81, 100

Combining two NumericRanges

You can combine elements from two NumericRanges using the zip() method. This is used for processing pairs of elements from two ranges simultaneously.

Example

Try following example for combining elements of two given ranges -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Two ranges of integers
    val range1: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
    val range2: NumericRange.Inclusive[Int] = NumericRange.inclusive(11, 20, 1)
    // Zip the two ranges together
    val zippedRange = range1.zip(range2)
    // Combined elements of given ranges
    println(zippedRange.mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

(1,11), (2,12), (3,13), (4,14), (5,15), (6,16), (7,17), (8,18), (9,19), (10,20)

Sum of Elements of NumericRanges

You can sum the elements of a NumericRange using the sum method. This is used for computing the total of all elements in the range.

Example

Try following example for sum of elements of given range 1 to 10 -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1 to 10
    val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
    // Sum of the elements
    val sum = range.sum
    // Print the sum
    println("Sum of range: " + sum)
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Sum of range: 55

Creating Ranges with Floating-Point Numbers

You can also create floating-point numbers using NumericRanges. So there will be more precise sequences with decimal values.

Example

Try following example for creating range with floating point numbers with given step 0.1 -

import scala.collection.immutable.NumericRange
import scala.math.BigDecimal.double2bigDecimal
import scala.collection.immutable.Range.BigDecimal.bigDecAsIntegral

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1.0 to 2.0 with step value 0.1
    val range: NumericRange[BigDecimal] = NumericRange.inclusive(BigDecimal(1.0), BigDecimal(2.0), BigDecimal(0.1))
    // Convert BigDecimal to Double and print the elements of the range
    println(range.map(_.toDouble).mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0

Finding Max and Min Values

You can find the maximum and minimum values in a NumericRange using the max and min methods. These methods return the highest and lowest values in the range, respectively.

Example

Try following example for finding max and min elements on the given range from 1 to 10 -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1 to 10
    val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
    // Find the maximum and minimum values in the range
    val maxValue = range.max
    val minValue = range.min
    // Maximum and minimum values
    println("Maximum value: " + maxValue)
    println("Minimum value: " + minValue)
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Maximum value: 10 
Minimum value: 1

Converting NumericRanges to Lists

You can convert a NumericRange to a List using the toList method. This is used when you need to work with the range as a list of elements.

Example

Try following example for converting given range elements into list elements -

import scala.collection.immutable.NumericRange

object Demo {
  def main(args: Array[String]): Unit = {
    // Range from 1 to 10
    val range: NumericRange.Inclusive[Int] = NumericRange.inclusive(1, 10, 1)
    // Convert the range to a list
    val rangeList = range.toList
    // Elements of the list
    println(rangeList.mkString(", "))
  }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

NumericRange Summary

  • You can create and manipulate sequences of numbers using NumericRange in Scala.
  • You can generate sequences with specific start, end, and step values using NumericRanges.
  • You can also filter, transform, zip, and sum elements of NumericRanges using various methods.
  • You can create ranges with both integer and floating-point numbers.
  • NumericRange is immutable collection in Scala.
Advertisements