An assortment of Kotlin extension functions.
Make a copy of a List
that contains a new element
or no longer contains that element dependent on whether it was
part of the collection before.
val list = listOf(1, 2, 3)
list.toggle(2) // [1, 3]
list.toggle(5) // [1, 2, 3, 5]
Make a copy of a List
with an added element or return
the unmodified collection if the element to be added is null.
val list = listOf(1, 2)
list.plusIfNotNull(3) // [1, 2, 3]
list.plusIfNotNull(null) // [1, 2]
Make a copy of a List
replace elements that match a predicate
with a provided element.
val list = listOf(1, 2, 3, 4)
list.replaceWith(0) { it % 2 == 0 } // [1, 0, 3, 0]
dependencies {
implementation "cc.femto:kotlin-extensions:0.3"
}
Requires the JitPack repository:
repositories {
maven { url "https://jitpack.io" }
}