Add additional removeFirst() variants to IterableUtils

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent 5d8dce0ed2
commit 09f17d246b
  1. 16
      util/src/main/java/dev/openrs2/util/collect/IterableUtils.kt

@ -1,5 +1,21 @@
package dev.openrs2.util.collect package dev.openrs2.util.collect
fun <T> MutableIterable<T>.removeFirst(): T {
return removeFirstOrNull() ?: throw NoSuchElementException()
}
fun <T> MutableIterable<T>.removeFirstOrNull(): T? {
val iterator = iterator()
if (!iterator.hasNext()) {
return null
}
val element = iterator.next()
iterator.remove()
return element
}
inline fun <T> MutableIterable<T>.removeFirst(predicate: (T) -> Boolean): Boolean { inline fun <T> MutableIterable<T>.removeFirst(predicate: (T) -> Boolean): Boolean {
val iterator = iterator() val iterator = iterator()
for (element in iterator) { for (element in iterator) {

Loading…
Cancel
Save