Zoltan Demant
08/10/2022, 10:51 AMif(contains) remove else add
but feel dirty everytime.Klitos Kyriacou
08/10/2022, 10:56 AMMutableSet
, you can do set.add(item) || set.remove(item)
. Not sure how readable that is, though.Zoltan Demant
08/10/2022, 11:01 AMAyfri
08/10/2022, 11:07 AMfun <T> MutableCollection<T>.toggle(property: T) {
if (property in this) remove(property) else add(property)
}
fun main() {
val myList = mutableListOf(1, 2, 3, 4)
myList.toggle(3)
myList.toggle(7)
println(myList)
}
// which prints :
// [1, 2, 4, 7]
Zoltan Demant
08/10/2022, 11:12 AMAyfri
08/10/2022, 11:13 AM