Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Kotlin Utility Function
Kotlin range utility functions have several standard library functions which are used in Kotlin ranges. These utility functions are as follow:
- rangeTo()
- downTo()
- reversed()
- step()
Kotlin rangeTo()
The rangeTo() function is used to return the value from start to end in increasing order mentioned in a range. The rangeTo() function is integral types which calls the constructors of Range class.
Example of rangeTo() function
fun main(args: Array) { var range: IntRange = 1.rangeTo(5) println("Printing value: 1.rangeTo(5)") for (x in range){ print("$x ") } println("") var range2: IntRange = IntRange(1,5) println("Printing value: IntRange(1,5)") for (x in range2){ print("$x ") } }
Output:
Printing value: 1.rangeTo(5) 1 2 3 4 5 Printing value: IntRange(1,5) 1 2 3 4 5
The data types (or types) which are floating point like Double, Float are not define in rangeTo operator.
Kotlin downTo()
The downTo() extension function is used to return the value in decreasing order mention from higher order to lower order. The downTo() function is defined for pair of integral types.
Syntax:
fun Long.downTo(other: Int): LongProgression { return LongProgression.fromClosedRange(this, other.toLong(), -1L) } fun Byte.downTo(other: Int): IntProgression { return IntProgression.fromClosedRange(this.toInt(), other, -1) }
Example of downTo() function
fun main(args: Array) { println("Range 1") var range1 = 5 downTo 1 for (x in range1){ print("$x ") } println() println("Range 2") var range2: IntProgression = 5.downTo(1) for (x in range2){ print("$x ") } println() println("Range 3") var range3: IntProgression = IntProgression.fromClosedRange(5,1,-1) for (x in range3){ print("$x ") } }
Output:
Range 1 5 4 3 2 1 Range 2 5 4 3 2 1 Range 3 5 4 3 2 1
Kotlin reversed()
The reversed() function is used to return the reversed order of the given range type.
Syntax:
fun IntProgression.reversed(): IntProgression { return IntProgression.fromClosedRange(last, first, -step) }
Example of reversed() function
fun main(args: Array) { println("Reversed 1") var range1 = 1..5 for (x in range1.reversed()){ print("$x ") } println() println("Reversed 2") var range2: IntRange = IntRange(1,5) for (x in range2.reversed()){ print("$x ") } println() println("Reversed 3") var range3 = IntProgression.fromClosedRange(5,1,-1) for (x in range3.reversed()){ print("$x ") } println() println("Reversed 4") var range4: IntProgression = IntProgression.fromClosedRange(5,1,-2) for (x in range4.reversed()){ print("$x ") } }
Output:
Reversed 1 5 4 3 2 1 Reversed 2 5 4 3 2 1 Reversed 3 1 2 3 4 5 Reversed 4 1 3 5
Kotlin step()
The step() function ( or step operator) is used to return the range value in interval of given step value. A step value always takes a positive parameter. The negative step value generates the IllegalArgumentException exception.
Example of step() function
fun main(args: Array) { val range: IntRange = 1..10 println("Print range value with step 2:") for(x in range step (2)){ print("$x ") } println("") println("Print range value with step 3:") for(x in range step 3){ print("$x ") } val first=((range step 2).first) val last=((range step 2).last) println("") println("First value of interval: $first") println("Last value of interval: $last ") }
Output:
Print range value with step 2: 1 3 5 7 9 Print range value with step 3: 1 4 7 10 First value of interval: 1 Last value of interval: 9
If we provide step value as a negative integer it throws an exception.
fun main(args: Array) { val range: IntRange = IntRange(1,10) for (x in range step -2){ print("$x ") } }
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Step must be positive, was: -2. at kotlin.ranges.RangesKt__RangesKt.checkStepIsPositive(Ranges.kt:130) at kotlin.ranges.RangesKt___RangesKt.step(_Ranges.kt:432) at TestKt.main(Test.kt:63)