Updated on 2026-08-14

This commit is contained in:
Tangem 2025-09-18 14:18:39 +04:00
parent 4193e5777e
commit 3306749ae5
11 changed files with 108 additions and 101 deletions

View file

@ -20,7 +20,22 @@ fun <T> Collection<T>.copy(): Collection<T> {
return this.map { it }
}
inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
val index = indexOfFirst(predicate)
return if (index == -1) null else index
/**
* Adds an element to the mutable list if the specified condition is true.
*
* @param condition The condition to evaluate.
* @param create A lambda function that creates the element to be added.
*/
inline fun <T> MutableCollection<T>.addIf(condition: Boolean, create: () -> T) {
addIf(condition = condition, element = create())
}
/**
* Adds an element to the mutable list if the specified condition is true.
*
* @param condition The condition to evaluate.
* @param element The element to be added.
*/
fun <T> MutableCollection<T>.addIf(condition: Boolean, element: T) {
if (condition) this.add(element)
}

View file

@ -72,4 +72,9 @@ fun <T> List<T>.filterIf(condition: Boolean, predicate: (T) -> Boolean): List<T>
} else {
this
}
}
inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
val index = indexOfFirst(predicate)
return if (index == -1) null else index
}