Updated on 2026-08-14
This commit is contained in:
parent
4193e5777e
commit
3306749ae5
11 changed files with 108 additions and 101 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue