I have an iterator that contains all the data I need in some sort order. I would like to perform some operation on each data element that takes into account the preceding and subsequent element in the iterator. I.e. I have a function that, given n elements, takes arguments f(iter(k-1), iter(k), iter(k+1))
for 0 < k < n-1
.
Of course, I cant just iterate over the iterator because I don't have access to the k + 1 element when I call the function.
I could just cast the whole thing do a list and use indexing, but that would be inelegant. What is the Scala way of accessing these values? Can I somehow compose the iterator with itself and an offset?