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
+99 -33
View File
@@ -1141,6 +1141,27 @@ class ArraysTest {
}
}
@Test
fun runningFold() {
for (size in 0 until 4) {
val expected = listOf("", "0", "01", "012", "0123").take(size + 1)
// Array<T>
assertEquals(expected, Array(size) { it }.runningFold("") { acc, e -> acc + e })
// Primitive Arrays
assertEquals(expected, ByteArray(size) { it.toByte() }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, CharArray(size) { '0' + it }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, ShortArray(size) { it.toShort() }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, IntArray(size) { it }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, LongArray(size) { it.toLong() }.runningFold("") { acc, e -> acc + e })
assertEquals(expected, FloatArray(size) { it.toFloat() }.runningFold("") { acc, e -> acc + e.toInt() })
assertEquals(expected, DoubleArray(size) { it.toDouble() }.runningFold("") { acc, e -> acc + e.toInt() })
assertEquals(
expected.map { it.map { c -> c.toInt() % 2 == 0 }.joinToString(separator = "") },
BooleanArray(size) { it % 2 == 0 }.runningFold("") { acc, e -> acc + e }
)
}
}
@Test
fun scanIndexed() {
for (size in 0 until 4) {
@@ -1185,92 +1206,137 @@ class ArraysTest {
)
}
}
@Test
fun scanReduce() {
fun runningFoldIndexed() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 3, 6).take(size)
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1)
// Array<T>
assertEquals(
expected,
Array(size) { it }.scanReduce { acc, e -> acc + e }
Array(size) { 'a' + it }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" }
)
// Primitive Arrays
assertEquals(
expected.map { it.toByte() },
ByteArray(size) { it.toByte() }.scanReduce { acc, e -> (acc + e).toByte() }
)
assertEquals(
expected.map { it.toChar() },
CharArray(size) { it.toChar() }.scanReduce { acc, e -> acc + e.toInt() }
)
assertEquals(
expected.map { it.toShort() },
ShortArray(size) { it.toShort() }.scanReduce { acc, e -> (acc + e).toShort() }
expected,
ByteArray(size) { it.toByte() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected,
IntArray(size) { it }.scanReduce { acc, e -> acc + e }
CharArray(size) { it.toChar() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.map { it.toLong() },
LongArray(size) { it.toLong() }.scanReduce { acc, e -> acc + e }
expected,
ShortArray(size) { it.toShort() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.map { it.toFloat() },
FloatArray(size) { it.toFloat() }.scanReduce { acc, e -> acc + e.toInt() }
expected,
IntArray(size) { it }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e}]" }
)
assertEquals(
expected.map { it.toDouble() },
DoubleArray(size) { it.toDouble() }.scanReduce { acc, e -> acc + e.toInt() }
expected,
LongArray(size) { it.toLong() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.indices.map { it % 2 == 0 },
BooleanArray(size) { true }.scanReduce { acc, e -> acc != e }
expected,
FloatArray(size) { it.toFloat() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected,
DoubleArray(size) { it.toDouble() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
)
assertEquals(
expected.map { it.map { c -> if (c.isAsciiLetter()) c.toInt() % 2 != 0 else c }.joinToString(separator = "") },
BooleanArray(size) { it % 2 == 0 }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" }
)
}
}
@Test
fun scanReduceIndexed() {
fun runningReduce() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 3, 6).take(size)
// Array<T>
assertEquals(
expected,
Array(size) { it }.runningReduce { acc, e -> acc + e }
)
// Primitive Arrays
assertEquals(
expected.map { it.toByte() },
ByteArray(size) { it.toByte() }.runningReduce { acc, e -> (acc + e).toByte() }
)
assertEquals(
expected.map { it.toChar() },
CharArray(size) { it.toChar() }.runningReduce { acc, e -> acc + e.toInt() }
)
assertEquals(
expected.map { it.toShort() },
ShortArray(size) { it.toShort() }.runningReduce { acc, e -> (acc + e).toShort() }
)
assertEquals(
expected,
IntArray(size) { it }.runningReduce { acc, e -> acc + e }
)
assertEquals(
expected.map { it.toLong() },
LongArray(size) { it.toLong() }.runningReduce { acc, e -> acc + e }
)
assertEquals(
expected.map { it.toFloat() },
FloatArray(size) { it.toFloat() }.runningReduce { acc, e -> acc + e.toInt() }
)
assertEquals(
expected.map { it.toDouble() },
DoubleArray(size) { it.toDouble() }.runningReduce { acc, e -> acc + e.toInt() }
)
assertEquals(
expected.indices.map { it % 2 == 0 },
BooleanArray(size) { true }.runningReduce { acc, e -> acc != e }
)
}
}
@Test
fun runningReduceIndexed() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 6, 27).take(size)
// Array<T>
assertEquals(
expected,
Array(size) { it }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
Array(size) { it }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
)
// Primitive Arrays
assertEquals(
expected.map { it.toByte() },
ByteArray(size) { it.toByte() }.scanReduceIndexed { index, acc, e -> (index * (acc + e)).toByte() })
ByteArray(size) { it.toByte() }.runningReduceIndexed { index, acc, e -> (index * (acc + e)).toByte() })
assertEquals(
expected.map { it.toChar() },
CharArray(size) { it.toChar() }.scanReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() }
CharArray(size) { it.toChar() }.runningReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() }
)
assertEquals(
expected.map { it.toShort() },
ShortArray(size) { it.toShort() }.scanReduceIndexed { index, acc, e -> (index * (acc + e)).toShort() }
ShortArray(size) { it.toShort() }.runningReduceIndexed { index, acc, e -> (index * (acc + e)).toShort() }
)
assertEquals(
expected,
IntArray(size) { it }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
IntArray(size) { it }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
)
assertEquals(
expected.map { it.toLong() },
LongArray(size) { it.toLong() }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
LongArray(size) { it.toLong() }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
)
assertEquals(
expected.map { it.toFloat() },
FloatArray(size) { it.toFloat() }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
FloatArray(size) { it.toFloat() }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
)
assertEquals(
expected.map { it.toDouble() },
DoubleArray(size) { it.toDouble() }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
DoubleArray(size) { it.toDouble() }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
)
assertEquals(
expected.indices.map { it % 2 == 0 },
BooleanArray(size) { true }.scanReduceIndexed { index, acc, e -> acc != e && index % 2 == 0 }
BooleanArray(size) { true }.runningReduceIndexed { index, acc, e -> acc != e && index % 2 == 0 }
)
}
}
@@ -397,6 +397,7 @@ class CollectionTest {
for (size in 0 until 4) {
val expected = listOf("", "0", "01", "012", "0123").take(size + 1)
assertEquals(expected, List(size) { it }.scan("") { acc, e -> acc + e })
assertEquals(expected, List(size) { it }.runningFold("") { acc, e -> acc + e })
}
}
@@ -405,22 +406,23 @@ class CollectionTest {
for (size in 0 until 4) {
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1)
assertEquals(expected, List(size) { 'a' + it }.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
assertEquals(expected, List(size) { 'a' + it }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
}
}
@Test
fun scanReduce() {
fun runningReduce() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 3, 6).take(size)
assertEquals(expected, List(size) { it }.scanReduce { acc, e -> acc + e })
assertEquals(expected, List(size) { it }.runningReduce { acc, e -> acc + e })
}
}
@Test
fun scanReduceIndexed() {
fun runningReduceIndexed() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 6, 27).take(size)
assertEquals(expected, List(size) { it }.scanReduceIndexed { index, acc, e -> index * (acc + e) })
assertEquals(expected, List(size) { it }.runningReduceIndexed { index, acc, e -> index * (acc + e) })
}
}
@@ -166,41 +166,35 @@ public class SequenceTest {
@Test
fun scan() {
for (size in 0 until 4) {
assertEquals(
sequenceOf("", "0", "01", "012").take(size).toList(),
indexSequence().scan("") { acc, e -> acc + e }.take(size).toList()
)
val expected = listOf("_", "_0", "_01", "_012").take(size)
assertEquals(expected, indexSequence().scan("_") { acc, e -> acc + e }.take(size).toList())
assertEquals(expected, indexSequence().runningFold("_") { acc, e -> acc + e }.take(size).toList())
}
}
@Test
fun scanIndexed() {
for (size in 0 until 4) {
assertEquals(
sequenceOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]").take(size).toList(),
indexSequence().map { 'a' + it }.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" }.take(size).toList()
)
val source = indexSequence().map { 'a' + it }
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]").take(size)
assertEquals(expected, source.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" }.take(size).toList())
assertEquals(expected, source.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" }.take(size).toList())
}
}
@Test
fun scanReduce() {
fun runningReduce() {
for (size in 0 until 4) {
val expected = listOf(0, 1, 3, 6).subList(0, size)
assertEquals(
sequenceOf(0, 1, 3, 6).take(size).toList(),
indexSequence().scanReduce { acc, e -> acc + e }.take(size).toList()
)
assertEquals(expected, indexSequence().runningReduce { acc, e -> acc + e }.take(size).toList())
}
}
@Test
fun scanReduceIndexed() {
fun runningReduceIndexed() {
for (size in 0 until 4) {
assertEquals(
sequenceOf(0, 1, 6, 27).take(size).toList(),
indexSequence().scanReduceIndexed { index, acc, e -> index * (acc + e) }.take(size).toList()
)
val expected = listOf(0, 1, 6, 27).take(size)
assertEquals(expected, indexSequence().runningReduceIndexed { index, acc, e -> index * (acc + e) }.take(size).toList())
}
}
@@ -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) }
)
}
}
+15 -17
View File
@@ -1359,40 +1359,38 @@ class StringTest {
@Test
fun scan() = withOneCharSequenceArg { arg1 ->
for (size in 0 until 4) {
assertEquals(
listOf("", "0", "01", "012", "0123").take(size + 1),
arg1((0 until size).joinToString(separator = "")).scan("") { acc, e -> acc + e }
)
val source = arg1((0 until size).joinToString(separator = ""))
val expected = listOf("", "0", "01", "012", "0123").take(size + 1)
assertEquals(expected, source.scan("") { acc, e -> acc + e })
assertEquals(expected, source.runningFold("") { acc, e -> acc + e })
}
}
@Test
fun scanIndexed() = withOneCharSequenceArg { arg1 ->
for (size in 0 until 4) {
assertEquals(
listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1),
arg1(('a' until 'a' + size).joinToString(separator = "")).scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" }
)
val source = arg1(('a' until 'a' + size).joinToString(separator = ""))
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1)
assertEquals(expected, source.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
assertEquals(expected, source.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
}
}
@Test
fun scanReduce() = withOneCharSequenceArg { arg1 ->
fun runningReduce() = withOneCharSequenceArg { arg1 ->
for (size in 0 until 4) {
assertEquals(
listOf(0, 1, 3, 6).take(size).map { it.toChar() },
arg1((0.toChar() until size.toChar()).joinToString(separator = "")).scanReduce { acc, e -> acc + e.toInt() }
)
val expected = listOf(0, 1, 3, 6).take(size).map { it.toChar() }
val source = arg1((0.toChar() until size.toChar()).joinToString(separator = ""))
assertEquals(expected, source.runningReduce { acc, e -> acc + e.toInt() })
}
}
@Test
fun scanReduceIndexed() = withOneCharSequenceArg { arg1 ->
for (size in 0 until 4) {
assertEquals(
listOf(0, 1, 6, 27).take(size).map { it.toChar() },
arg1((0.toChar() until size.toChar()).joinToString(separator = "")).scanReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() }
)
val expected = listOf(0, 1, 6, 27).take(size).map { it.toChar() }
val source = arg1((0.toChar() until size.toChar()).joinToString(separator = ""))
assertEquals(expected, source.runningReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() })
}
}