don't generate two 'flatMap' methods(*) for Iterator

* with the same erased signature
 #KT-3373 Fixed
This commit is contained in:
Svetlana Isakova
2013-03-22 20:07:23 +04:00
parent d0e736e5a6
commit 362770c9b1
8 changed files with 23 additions and 25 deletions
@@ -35,6 +35,13 @@ public inline fun <T, R> Collection<T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*/
public inline fun <T, R> Collection<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns a list containing the first *n* elements
*/
@@ -90,13 +90,6 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.mapTo(result: C
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*/
public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
@@ -90,13 +90,6 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.mapTo(result: C
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*/
public inline fun <T, R> Iterator<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
@@ -14,7 +14,7 @@ class IteratorsJVMTest {
iterate<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayList(
val expected = arrayListOf(
'0', // fibonacci(0) = 0
'1', // fibonacci(1) = 1
'1', // fibonacci(2) = 1
@@ -25,4 +25,9 @@ class IteratorsJVMTest {
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
}
test fun flatMapOnIterator() {
val result = listOf(1, 2).iterator().flatMap { i -> (0..i).iterator()}
assertEquals(listOf(0, 1, 0, 1, 2), result.toList())
}
}
@@ -35,7 +35,7 @@ fun main(args: Array<String>) {
buildFor(Iterators, "")
}
val iteratorSignatures = templates.map { it.signature.flat() }.toSet()
val iteratorSignatures = templates.map { it.erasedSignature.flat() }.toSet()
templates.clear()
collections()
@@ -50,15 +50,15 @@ fun main(args: Array<String>) {
}
templates.writeTo(File(outDir, "_Iterables.kt")) {
if (iteratorSignatures contains signature.flat()) "" else buildFor(Iterables, "")
if (iteratorSignatures contains erasedSignature.flat()) "" else buildFor(Iterables, "")
}
templates.writeTo(File(outDir, "_IteratorsCommon.kt")) {
if (iteratorSignatures contains signature.flat()) "" else buildFor(Iterators, "")
if (iteratorSignatures contains erasedSignature.flat()) "" else buildFor(Iterators, "")
}
templates.writeTo(File(outDir, "_Collections.kt")) {
if (iteratorSignatures contains signature.flat()) buildFor(Collections, "") else ""
if (iteratorSignatures contains erasedSignature.flat()) buildFor(Collections, "") else ""
}
generateDownTos(File(outDir, "_DownTo.kt"), "package kotlin")
@@ -179,7 +179,7 @@ fun collections() {
}
}
f("flatMap(transform: (T)-> Iterable<R>)") {
f("flatMap(transform: (T)-> Iterable<R>)", "flatMap(Function1)") {
doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single list"
typeParam("R")
returns("List<R>")
@@ -581,4 +581,4 @@ fun collections() {
"""
}
}
}
}
@@ -15,7 +15,7 @@ enum class Family {
PrimitiveArrays
}
class GenericFunction(val signature : String) {
class GenericFunction(val signature : String, val erasedSignature: String) {
var doc : String = ""
var toNullableT : Boolean = false
val isInline : Boolean = true;
@@ -151,8 +151,8 @@ fun String.trimTrailingSpaces() : String {
val templates = ArrayList<GenericFunction>()
fun f(signature : String, init : GenericFunction.() -> Unit) {
val gf = GenericFunction(signature)
fun f(signature : String, erasedSignature: String = signature, init : GenericFunction.() -> Unit) {
val gf = GenericFunction(signature, erasedSignature)
gf.init()
templates.add(gf)
}
@@ -40,7 +40,7 @@ fun iterators() {
}
}
f("flatMap(transform: (T) -> Iterator<R>)") {
f("flatMap(transform: (T) -> Iterator<R>)", "flatMap(Function1)") {
doc = "Returns an iterator over the concatenated results of transforming each element to one or more values"
typeParam("R")
returns("Iterator<R>")