Introduce runningFold and runningReduce operations

runningFold is a synonym for scan,
runningReduce replaces scanReduce.

#KT-38060
This commit is contained in:
Ilya Gorbunov
2020-04-14 14:34:02 +03:00
parent 37277d71de
commit 893021f22b
13 changed files with 1551 additions and 516 deletions
@@ -674,6 +674,17 @@ class UnsignedArraysTest {
}
}
@Test
fun runningFold() {
for (size in 0 until 4) {
val expected = listOf("", "0", "01", "012", "0123").subList(0, size + 1)
assertEquals(expected, UByteArray(size) { it.toUByte() }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, UShortArray(size) { it.toUShort() }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, UIntArray(size) { it.toUInt() }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, ULongArray(size) { it.toULong() }.runningFold("") { acc, e -> acc + e })
}
}
@Test
fun scanIndexed() {
for (size in 0 until 4) {
@@ -698,47 +709,70 @@ class UnsignedArraysTest {
}
@Test
fun scanReduce() {
fun runningFoldIndexed() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 3, 6).subList(0, size)
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").subList(0, size + 1)
assertEquals(
expected.map { it.toUByte() },
UByteArray(size) { it.toUByte() }.scanReduce { acc, e -> (acc + e).toUByte() }
expected,
UByteArray(size) { it.toUByte() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.map { it.toUShort() },
UShortArray(size) { it.toUShort() }.scanReduce { acc, e -> (acc + e).toUShort() }
expected,
UShortArray(size) { it.toUShort() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.map { it.toUInt() },
UIntArray(size) { it.toUInt() }.scanReduce { acc, e -> acc + e }
expected,
UIntArray(size) { it.toUInt() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.map { it.toULong() },
ULongArray(size) { it.toULong() }.scanReduce { acc, e -> acc + e }
expected,
ULongArray(size) { it.toULong() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
}
}
@Test
fun scanReduceIndexed() {
fun runningReduce() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 3, 6).subList(0, size)
assertEquals(
expected.map { it.toUByte() },
UByteArray(size) { it.toUByte() }.runningReduce { acc, e -> (acc + e).toUByte() }
)
assertEquals(
expected.map { it.toUShort() },
UShortArray(size) { it.toUShort() }.runningReduce { acc, e -> (acc + e).toUShort() }
)
assertEquals(
expected.map { it.toUInt() },
UIntArray(size) { it.toUInt() }.runningReduce { acc, e -> acc + e }
)
assertEquals(
expected.map { it.toULong() },
ULongArray(size) { it.toULong() }.runningReduce { acc, e -> acc + e }
)
}
}
@Test
fun runningReduceIndexed() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 6, 27).subList(0, size)
assertEquals(
expected.map { it.toUByte() },
UByteArray(size) { it.toUByte() }.scanReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUByte() }
UByteArray(size) { it.toUByte() }.runningReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUByte() }
)
assertEquals(
expected.map { it.toUShort() },
UShortArray(size) { it.toUShort() }.scanReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUShort() }
UShortArray(size) { it.toUShort() }.runningReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUShort() }
)
assertEquals(
expected.map { it.toUInt() },
UIntArray(size) { it.toUInt() }.scanReduceIndexed { index, acc, e -> index.toUInt() * (acc + e) }
UIntArray(size) { it.toUInt() }.runningReduceIndexed { index, acc, e -> index.toUInt() * (acc + e) }
)
assertEquals(
expected.map { it.toULong() },
ULongArray(size) { it.toULong() }.scanReduceIndexed { index, acc, e -> index.toULong() * (acc + e) }
ULongArray(size) { it.toULong() }.runningReduceIndexed { index, acc, e -> index.toULong() * (acc + e) }
)
}
}