LastExceed
05/06/2020, 12:54 PMfor (x in y)
or do i have to remove them manually ?David Eriksson
05/06/2020, 12:59 PMLastExceed
05/06/2020, 12:59 PMDavid Eriksson
05/06/2020, 1:02 PMdiesieben07
05/06/2020, 1:02 PMval collection = mutableListOf(1, 2, 3)
val iterator = collection.iterator()
for (element in iterator) {
if (element % 2 == 0) {
iterator.remove()
}
}
FranSoto
05/06/2020, 1:03 PMLastExceed
05/06/2020, 1:04 PMDavid Eriksson
05/06/2020, 1:04 PMwhile (!queue.isEmpty()) {
val item = queue.pop();
// do stufff
}
LastExceed
05/06/2020, 1:06 PMLastExceed
05/06/2020, 1:08 PMfor (element in collection) {
collection.remove()
//do sth
}
but it seems fishy somehowDavid Eriksson
05/06/2020, 1:08 PMLastExceed
05/06/2020, 1:13 PMval iterator = collection.iterator()
for (element in iterator) {
iterator.remove()
//do sth
}
i still get the impression that there's gotta be a better way to do thisFranSoto
05/06/2020, 1:30 PMcristiangm
05/06/2020, 1:43 PMJakub Pi
05/06/2020, 1:55 PMpoll()
inside a while loop. In Kotlin, I'd avoid the use of mutable collections. Depends on the usecase.LastExceed
05/06/2020, 2:02 PMDerek Peirce
05/07/2020, 1:58 AMIterable.removingForEach { ... }
extension method that does this. I don't believe any existing stdlib method will accomplish this. Java 8 has removeIf
, but it won't be inlined, and you don't need the extra conditional evaluation of removeIf { true }
diesieben07
05/07/2020, 8:10 AMremoveIf
also has no guarantees about visiting each element only once and no guarantees about visiting them in order. The predicate you give it should be a pure functionJakub Pi
05/07/2020, 6:47 PMdropWhile()
and pass it the inverse of your condition.Derek Peirce
05/08/2020, 4:27 AMdropWhile
will not modify the original queue, it will create a new list containing the elements that were not removed.