Chilli
11/27/2020, 9:42 PMDominaezzz
11/27/2020, 9:43 PMDominaezzz
11/27/2020, 9:44 PMDominaezzz
11/27/2020, 9:44 PMChilli
11/27/2020, 9:49 PMDominaezzz
11/27/2020, 9:50 PMDominaezzz
11/27/2020, 9:51 PMzip
might help?Tobias Berger
11/27/2020, 9:51 PMprevious()
function. Peeking (looking at the next element without progressing) is not included, which might be a good thing because in some cases it could lead to unexpected behaviour. However, you could implement your own "PeakingIterator" (or use the one from apache commons if you're on JVM)Nir
11/27/2020, 9:55 PMNir
11/27/2020, 9:55 PMChilli
11/27/2020, 9:56 PMTobias Berger
11/27/2020, 10:05 PMNir
11/27/2020, 10:09 PMokarm
11/27/2020, 10:10 PMTobias Berger
11/27/2020, 10:10 PMpeek()
and there are no more elements. A check for that could be added easily if you want that behaviour
@Nir you're not having a stroke (I hope), I just had a brain fart when writing the classNir
11/27/2020, 10:10 PMNir
11/27/2020, 10:10 PMNir
11/27/2020, 10:11 PMTobias Berger
11/27/2020, 10:14 PMAbstractIterator
implementation and exposing nextValue
(or something that would look very similar). I wouldn't have written it like above myself, but it is shorter and part of a widely used Google lib, it should be ok.Tobias Berger
11/27/2020, 10:15 PMNir
11/27/2020, 10:17 PMclass PeekingIterator<T>(private val iterator: Iterator<T>) : Iterator<T> {
fun Iterator<T>.nextOrNull() = if (iterator.hasNext()) iterator.next() else null
var nextElement = iterator.nextOrNull()
override fun hasNext() = (nextElement != null)
override fun next(): T {
val next = nextElement!!
nextElement = iterator.nextOrNull()
return next
}
fun peek() = nextElement
}
Tobias Berger
11/27/2020, 10:19 PMPeakingIterator<T: Any>
(not allowing nullable types). Having null values would break this implementationNir
11/27/2020, 10:20 PMTobias Berger
11/27/2020, 10:20 PMoverride fun next(): T {
val next = nextElement ?: throw NoSuchElementException()
...
Nir
11/27/2020, 10:20 PMNir
11/27/2020, 10:21 PMNir
11/27/2020, 10:21 PMTobias Berger
11/27/2020, 10:22 PMnull
and undefined
Nir
11/27/2020, 10:25 PMTobias Berger
11/27/2020, 10:25 PMnull
for an optional parameter that also has null
as its default value. Same issue.Nir
11/27/2020, 10:25 PMNir
11/27/2020, 10:26 PMNir
11/27/2020, 10:26 PMTobias Berger
11/27/2020, 10:27 PMnull
can actually be an expected value.Tobias Berger
11/27/2020, 10:29 PM?.let
, ?:
, etc.Tobias Berger
11/27/2020, 10:30 PMNir
11/27/2020, 10:34 PMNir
11/27/2020, 10:34 PMTobias Berger
11/27/2020, 10:38 PMNir
11/27/2020, 10:39 PM