solution of #489

This commit is contained in:
InsanusMokrassar 2024-10-01 20:30:45 +06:00
parent bfb6e738ee
commit 2f70a1cfb4
3 changed files with 28 additions and 0 deletions

View File

@ -2,6 +2,8 @@
## 0.22.5 ## 0.22.5
* `Common`:
* Add extension `withReplacedAt`/`withReplaced` ([#489](https://github.com/InsanusMokrassar/MicroUtils/issues/489))
* `Coroutines`: * `Coroutines`:
* Add extension `Flow.debouncedBy` * Add extension `Flow.debouncedBy`

View File

@ -0,0 +1,5 @@
package dev.inmo.micro_utils.common
fun <T> Iterable<T>.withReplacedAt(i: Int, block: (T) -> T): List<T> = take(i) + block(elementAt(i)) + drop(i + 1)
fun <T> Iterable<T>.withReplaced(t: T, block: (T) -> T): List<T> = withReplacedAt(indexOf(t), block)

View File

@ -0,0 +1,21 @@
package dev.inmo.micro_utils.common
import kotlin.test.Test
import kotlin.test.assertEquals
class WithReplacedTest {
@Test
fun testReplaced() {
val data = 0 until 10
val testData = Int.MAX_VALUE
for (i in 0 until data.last) {
val withReplaced = data.withReplacedAt(i) {
testData
}
val dataAsMutableList = data.toMutableList()
dataAsMutableList[i] = testData
assertEquals(withReplaced, dataAsMutableList.toList())
}
}
}