Groovy Closed And Open Ranges Code Example

The code for Groovy Closed And Open Ranges

def inclRange = 0..5
 
assert inclRange == [0, 1, 2, 3, 4, 5]
assert inclRange.from == 0
assert inclRange.to == 5
 
def exclEndRange = 0..<5
 
assert exclEndRange == [0, 1, 2, 3, 4]
assert exclEndRange.from == 0
assert exclEndRange.to == 4
 
// Support for exclusive begin added in Groovy 4.
def exclBeginRange = 0<..5
 
assert exclBeginRange == [1, 2, 3, 4, 5]
assert exclBeginRange.from == 1
assert exclBeginRange.to == 5
 
// Support for exclusive begin added in Groovy 4.
def exclRange = 0<..<5
 
assert exclRange == [1, 2, 3, 4]
assert exclRange.from == 1
assert exclRange.to == 4
Code by Shivang Yadav, on July 24, 2022
Reference: blog.mrhaki.com

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.