I have a need to drop quantities of elements from an iterator beyond Int.MaxValue
so I wrote a helper function that does just that. However, I can't help but feel like I'm doing this the dumb way - that there's got to be some built-in language construct or at least a more elegant way of tackling the problem.
/**
* drops a Long's worth of elements from an Iterator[T] and returns the succeeding element.
*
* @param dropVal is the number of elements we want to drop
* @param iteratorLong is the the iterator from which you want to drop lots-o-elements
*
* @return the value you need
*/
def dropLongAndGet[T](dropVal: Long, iteratorLong: Iterator[T]): T = {
val dropCount = dropVal / Int.MaxValue
val dropCountRemainder = (dropVal % Int.MaxValue).toInt
var localIteratorReference = iteratorLong
for (i <- 0L until dropCount) {
localIteratorReference = iteratorLong.drop(Int.MaxValue)
}
return localIteratorReference.drop(dropCountRemainder)
.next
}
Also, I am aware that the code will work w/o the return
keyword - I use it because I'm used to seeing it from Java, it doesn't hurt anything to have it there, and I think it helps non-Scala programmers make sense of what's going on if/when they have to read my code.