Thursday, 17 January 2019

Groovy: Range (..) operator


Range operator (..) is used to create range of objects. Range elements are represented as list internally.

Example
range = 0..10

Above statement defines a range of integers from 0 to 10. Ranges usually has a lower left bound and higher right bound.

HelloWorld.groovy
def range = 0..10                                   

if(range instanceof List){
 println "Range is an instance of list"
 println range
}              

Output
Range is an instance of list
0..10

Get number of elements in the range
Use the 'size' method to get the number of elements in the range.


HelloWorld.groovy
def range = 0..10                                   

if(range instanceof List){
 println "Range is an instance of list"
 println range
 println "Number of Elements in the range ${range.size()}"
}              

Output
Range is an instance of list
0..10
Number of Elements in the range 11

..< operator
It specifies that the value on the right side is not part of the range.


HelloWorld.groovy
def range = 0..<10

for (i in range){
 println i
}

Output
0
1
2
3
4
5
6
7
8
9

As you see the output, ..< operator is not included 10 in the range.

You can apply ranges to other types like character and dates etc.,


HelloWorld.groovy
def range = 'a'..'z'

for (i in range){
 print " " + i
}

println ""

def today = new Date()
def tenDaysBefore = today-10
def tenDays = tenDaysBefore..today

for (i in tenDays){
 print " " + i
}

Output
a b c d e f g h i j k l m n o p q r s t u v w x y z
 Sat Nov 10 20:28:09 IST 2018 Sun Nov 11 20:28:09 IST 2018 Mon Nov 12 20:28:09 IST 2018 Tue Nov 13 20:28:09 IST 2018 Wed Nov 14 20:28:09 IST 2018 Thu Nov 15 20:28:09 IST 2018 Fri Nov 16 20:28:09 IST 2018 Sat Nov 17 20:28:09 IST 2018 Sun Nov 18 20:28:09 IST 2018 Mon Nov 19 20:28:09 IST 2018 Tue Nov 20 20:28:09 IST 2018

Reverse Range
Reverse ranges also possible in Groovy


HelloWorld.groovy
def range = 10..1

for (i in range){
 print i + " "
}

Output
10 9 8 7 6 5 4 3 2 1

Iterate over range elements
By using 'each' method (or) for-in loop you can iterate over the elements of the range.


HelloWorld.groovy
def range = 10..1

for (element in range){
 print element + " "
}

println ""

range.each{ element -> print element + " "}

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

You can even use, ranges in switch case like below.


HelloWorld.groovy
int marks = 65

switch(marks){
 case 0..34 : println "failed"; break
 case 35..59 : println "Second Class"; break
 case 60..69 : println "First Class"; break
 case 70..100 : println "Distinction"; break
 default : println "invalid value"
}

Output
First Class

You can even grep the range of elements from a collection.


HelloWorld.groovy
def primes = [2, 3, 5, 7, 11, 13, 17, 23, 29, 31, 37, 41, 43, 47]

def range = 15..40

def primesBeetween = primes.grep(range)

println primesBeetween

Output
[17, 23, 29, 31, 37]



Previous                                                 Next                                                 Home

No comments:

Post a Comment