Introduce new overloads of flatMap and flatMapTo

- Sequence<T>.flatMap((T) -> Iterable<R>)
- Iterable<T>.flatMap((T) -> Sequence<R>)
- Array<T>.flatMap((T) -> Sequence<R>)
- Map.flatMap((Entry) -> Sequence<R>)

KT-34506
This commit is contained in:
Ilya Gorbunov
2020-04-24 05:15:07 +03:00
parent 562788ceb9
commit bdd53ee9cd
10 changed files with 233 additions and 9 deletions
@@ -12,6 +12,7 @@ import test.assertTypeEquals
import test.collections.behaviors.*
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
import test.text.isAsciiLetter
import kotlin.math.exp
import kotlin.test.*
import kotlin.random.Random
@@ -1756,6 +1757,15 @@ class ArraysTest {
assertEquals(listOf(2), arrayOf("a", null, "test").mapIndexedNotNull { index, it -> it?.run { if (index != 0) length / index else null } })
}
@Test fun flatMap() {
val data = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6))
val result1 = data.flatMap { it.asList() }
val result2 = data.flatMap { it.asSequence() }
val expected = (data[0] + data[1]).toList()
assertEquals(expected, result1)
assertEquals(expected, result2)
}
@Test fun flattenArray() {
val arr1: Array<Array<Int>> = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6))
val arr2: Array<out Array<Int>> = arr1