Updated on 2026-08-14

This commit is contained in:
Tangem 2023-08-03 16:28:14 +03:00
parent cd78832ff1
commit c4fcd17bef
2 changed files with 18 additions and 4 deletions

View file

@ -53,11 +53,12 @@ inline fun <T> MutableCollection<T>.addOrReplace(item: T, predicate: (T) -> Bool
/**
* Removes an element from the collection based on the provided predicate.
* Uses iterator, avoid using it in COW collections
*
* @param predicate The condition to remove an element.
* @return [Boolean] indicating whether an element was removed.
*/
inline fun <T> MutableCollection<T>.removeBy(predicate: (T) -> Boolean): Boolean {
inline fun <T> MutableCollection<T>.removeByIterate(predicate: (T) -> Boolean): Boolean {
var removed = false
val iterator = this.iterator()
@ -73,6 +74,19 @@ inline fun <T> MutableCollection<T>.removeBy(predicate: (T) -> Boolean): Boolean
return removed
}
/**
* Removes an element from the collection based on the provided predicate.
* Uses removeAll() method and could be used for COW collections
*
* @param predicate The condition to remove an element.
* @return [Boolean] indicating whether an element was removed.
*/
fun <T> MutableList<T>.removeByReplace(predicate: (T) -> Boolean): Boolean {
val toRemove = this.filter(predicate)
this.removeAll(toRemove)
return toRemove.isNotEmpty()
}
/**
* Replaces an element in the collection with the provided item based on the predicate.
*