Skip to content

Commit

Permalink
solution of #489
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Oct 1, 2024
1 parent bfb6e73 commit 2f70a1c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
@@ -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)

Original file line number Diff line number Diff line change
@@ -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())
}
}
}

0 comments on commit 2f70a1c

Please sign in to comment.