stdlib: add flatten()
This commit is contained in:
@@ -79,10 +79,6 @@ public fun <K, V: Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K
|
||||
|
||||
public fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
|
||||
|
||||
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
||||
return flatMapTo(ArrayList<T>(), {it})
|
||||
}
|
||||
|
||||
public fun <T: Any> emptyOrSingletonList(item: T?): List<T> = if (item == null) listOf() else listOf(item)
|
||||
|
||||
public fun <T: Any> MutableCollection<T>.addIfNotNull(t: T?) {
|
||||
|
||||
+1
-2
@@ -43,7 +43,6 @@ import org.jetbrains.kotlin.idea.refactoring.move.getFileNameAfterMove
|
||||
import org.jetbrains.kotlin.psi.JetNamedDeclaration
|
||||
import org.jetbrains.kotlin.asJava.toLightElements
|
||||
import java.util.HashMap
|
||||
import org.jetbrains.kotlin.utils.flatten
|
||||
import java.util.ArrayList
|
||||
import java.util.HashSet
|
||||
import com.intellij.psi.PsiReference
|
||||
@@ -119,7 +118,7 @@ public class MoveKotlinTopLevelDeclarationsProcessor(
|
||||
val newPackageName = options.moveTarget.packageWrapper?.getQualifiedName() ?: ""
|
||||
|
||||
fun collectUsages(): List<UsageInfo> {
|
||||
return kotlinToLightElements.values().flatten().flatMap { lightElement ->
|
||||
return kotlinToLightElements.values().flatMap { it }.flatMap { lightElement ->
|
||||
val newFqName = StringUtil.getQualifiedName(newPackageName, lightElement.getName())
|
||||
|
||||
val foundReferences = HashSet<PsiReference>()
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package kotlin
|
||||
|
||||
import java.util.ArrayList
|
||||
import kotlin.platform.platformName
|
||||
|
||||
/**
|
||||
* Returns a single list of all elements from all collections in original collection
|
||||
*/
|
||||
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
||||
val result = ArrayList<T>()
|
||||
for (element in this) {
|
||||
result.addAll(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stream of all elements from all streams in original stream
|
||||
*/
|
||||
public fun <T> Stream<Stream<T>>.flatten(): Stream<T> {
|
||||
return FlatteningStream(this, { it })
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single list of all elements from all collections in original collection
|
||||
*/
|
||||
public fun <T> Array<Array<out T>>.flatten(): List<T> {
|
||||
val result = ArrayList<T>(sumBy { it.size() })
|
||||
for (element in this) {
|
||||
result.addAll(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -164,6 +164,11 @@ abstract class IterableTests<T : Iterable<String>>(val data: T, val empty: T) {
|
||||
assertEquals(listOf(3, 3), lengths)
|
||||
}
|
||||
|
||||
Test
|
||||
fun flatten() {
|
||||
assertEquals(listOf(0, 1, 2, 3, 0, 1, 2, 3), data.map { 0..it.length() }.flatten())
|
||||
}
|
||||
|
||||
Test
|
||||
fun mapIndexed() {
|
||||
val shortened = data.mapIndexed {(index, value) -> value.substring(0..index) }
|
||||
|
||||
@@ -213,6 +213,13 @@ public class StreamTest {
|
||||
assertEquals(listOf(0, 1, 3, 4), result.toList())
|
||||
}
|
||||
|
||||
test
|
||||
fun flatten() {
|
||||
val data = streamOf(1, 2).map { streamOf(0..it) }
|
||||
assertEquals(listOf(0, 1, 0, 1, 2), data.flatten().toList())
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
test fun pairIterator() {
|
||||
val pairStr = (fibonacci() zip fibonacci().map { i -> i*2 }).joinToString(limit = 10)
|
||||
|
||||
Reference in New Issue
Block a user