
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Methods
- Scala - Functions
- Scala - Methods vs Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala - Polymorphic Functions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - NumericRange
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala Advanced Types
- Scala - Union Types
- Scala - Intersection Types
- Scala - Type Aliases
- Scala - Structural Types
- Scala - Match Expression
- Scala - Singleton Type Operator
- Scala - Abstract Types
- Scala - Dependent Types
- Scala - Abstract Type Bounds
- Scala - Higher-Kinded Types
- Scala - Opaque Type Alias
- Scala - Path-Dependent Types
- Scala - Type Lambdas
- Scala - Type Inference
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Guards
- Scala - Variables in Patterns
- Scala - Type Patterns
- Scala - The Matchable Trait
- Scala - Matching Arrays
- Scala - Matching Lists
- Scala - Matching Tuples
- Scala - Exception Handling
- Scala - Extractors
- Scala - Pattern Bindings
- Scala - Regular Expressions
- Scala - Case Classes
- Scala - Partial Functions
- Scala - Packaging and Imports
- Scala - Implicit Imports
- Scala - Export Clauses
- Scala - Nested Packages
- Scala - Chained Packages
- Scala - Package Objects
- Scala Files I/O
- Scala - Files I/O
- Scala - Writing Files
- Scala - Listing Files
- Scala - Deleting Directories
- Scala - Check File Exists
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Process Management
- Scala - Scaladoc
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala Unit Testing
- Scala - Unit Testing
- Scala - uTest
- Scala - MUnit
- Scala - ScalaTest Runner
- Scala - ScalaMock
- Scala - JUnit
- Scala - Mocking
- Scala - BDD Testing
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.