tyrondis
03/30/2020, 9:55 AMval it = (1..3).circularIterator()
it.next() // 1
it.next() // 2
it.next() // 3
it.next() // 1
// and so on...
Any ideas how I can accomplish this? Is there already something similar in the stdlib?diesieben07
03/30/2020, 10:00 AMfun <R> Iterable<R>.circularIterator(): Iterator<R> {
return iterator {
while (true) {
yieldAll(this@circularIterator)
}
}
}
Comes to minddiesieben07
03/30/2020, 10:04 AMSequence
instead of an iterator though, in which case you can just replace iterator { }
with sequence { }
tyrondis
03/30/2020, 10:12 AMtyrondis
03/30/2020, 10:13 AMSequence
be better here?diesieben07
03/30/2020, 10:14 AMSequence
would allow you to further perform operations on the data (such as filter
, map
, etc.) and also allow iterating it multiple times.tyrondis
03/30/2020, 10:15 AM