mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
solution of #489
This commit is contained in:
parent
bfb6e738ee
commit
2f70a1cfb4
@ -2,6 +2,8 @@
|
||||
|
||||
## 0.22.5
|
||||
|
||||
* `Common`:
|
||||
* Add extension `withReplacedAt`/`withReplaced` ([#489](https://github.com/InsanusMokrassar/MicroUtils/issues/489))
|
||||
* `Coroutines`:
|
||||
* Add extension `Flow.debouncedBy`
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user