diff --git a/modules/collections/src/commonMain/kotlin/splitties/collections/Lists.kt b/modules/collections/src/commonMain/kotlin/splitties/collections/Lists.kt index 1e37367b8..f59ec26fb 100644 --- a/modules/collections/src/commonMain/kotlin/splitties/collections/Lists.kt +++ b/modules/collections/src/commonMain/kotlin/splitties/collections/Lists.kt @@ -100,3 +100,26 @@ inline fun List.forEachReversedWithIndex( action(i, get(i)) } } + +/** + * Execute an [action] to the first item witch matches the [predicate]. + * Do nothing if there's not match + */ +fun Collection.executeOnFirstMatch(predicate:(T) -> Boolean,action: (T) -> Unit) { + val matched = this.firstOrNull(predicate) + if (matched != null) { + action(matched) + } +} + +/** + * Toggle an [item] from a MutableList. + */ +fun MutableList.toggle(item: T): List { + if (this.contains(item)) { + this.remove(item) + } else { + this.add(item) + } + return this +}