Groovy way to find valid end of a range -
imagine have big list want split in smaller chunks processing:
def chunksize = 10 def listsize = abiglist.size() for(def = 0; < listsize; += chunksize) { def startofrange = def endofrange = (listsize - 1) < (i + increment - 1) ? (listsize - 1) : (i + increment - 1) // there has got better way! def chunk = abiglist[startofrange..endofrange] // chunk }
the code getting endofrange
variable ugly , non-groovy, required in order prevent index out of bounds
exception. there better way this?
can't use collate?
def chunks = abiglist.collate( chunksize )
if not, fixing code gives like:
def chunksize = 10 def listsize = abiglist.size() for( = 0; < listsize; += chunksize ) { def endofrange = + chunksize - 1 >= abiglist.size() ? -1 : + chunksize - 1 def chunk = abiglist[ i..endofrange ] // chunk }
but collate way go long abiglist
isn't enormous ;-)
Comments
Post a Comment