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
+16 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -216,6 +216,21 @@ class MapTest {
assertEquals(mapOf(8 to "Mells"), m3)
}
@Test fun flatMap() {
fun <T> list(entry: Map.Entry<T, T>): List<T> = listOf(entry.key, entry.value)
fun <T> seq(entry: Map.Entry<T, T>): Sequence<T> = sequenceOf(entry.key, entry.value)
val m = mapOf("x" to 1, "y" to 0)
val result1 = m.flatMap { list(it) }
val result2 = m.flatMap { seq(it) }
val result3 = m.flatMap(::list)
val result4 = m.flatMap(::seq)
val expected = listOf("x", 1, "y", 0)
assertEquals(expected, result1)
assertEquals(expected, result2)
assertEquals(expected, result3)
assertEquals(expected, result4)
}
@Test fun createFrom() {
val pairs = arrayOf("a" to 1, "b" to 2)
val expected = mapOf(*pairs)