Make zip operation symmetrical for CharSequences (missed to generalize parameter before).

This commit is contained in:
Ilya Gorbunov
2015-11-22 09:24:45 +03:00
parent a05d0a3c49
commit c485eda1c2
2 changed files with 20 additions and 3 deletions
+7 -2
View File
@@ -931,14 +931,14 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, S
/** /**
* Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence.
*/ */
public infix fun CharSequence.zip(other: String): List<Pair<Char, Char>> { public infix fun CharSequence.zip(other: CharSequence): List<Pair<Char, Char>> {
return zip(other) { c1, c2 -> c1 to c2 } return zip(other) { c1, c2 -> c1 to c2 }
} }
/** /**
* Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. * Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence.
*/ */
public inline fun <V> CharSequence.zip(other: String, transform: (Char, Char) -> V): List<V> { public inline fun <V> CharSequence.zip(other: CharSequence, transform: (Char, Char) -> V): List<V> {
val length = Math.min(this.length, other.length) val length = Math.min(this.length, other.length)
val list = ArrayList<V>(length) val list = ArrayList<V>(length)
for (i in 0..length-1) { for (i in 0..length-1) {
@@ -947,6 +947,11 @@ public inline fun <V> CharSequence.zip(other: String, transform: (Char, Char) ->
return list return list
} }
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public infix fun CharSequence.zip(other: String): List<Pair<Char, Char>> {
return zip(other) { c1, c2 -> c1 to c2 }
}
/** /**
* Returns a sequence from the given collection. * Returns a sequence from the given collection.
*/ */
@@ -555,7 +555,7 @@ fun generators(): List<GenericFunction> {
} }
} }
templates add f("zip(other: String, transform: (Char, Char) -> V)") { templates add f("zip(other: CharSequence, transform: (Char, Char) -> V)") {
only(CharSequences) only(CharSequences)
doc { doc {
""" """
@@ -597,6 +597,18 @@ fun generators(): List<GenericFunction> {
} }
templates add f("zip(other: String)") { templates add f("zip(other: String)") {
infix(true)
only(CharSequences)
deprecate { forBinaryCompatibility }
returns("List<Pair<Char, Char>>")
body {
"""
return zip(other) { c1, c2 -> c1 to c2 }
"""
}
}
templates add f("zip(other: CharSequence)") {
infix(true) infix(true)
only(CharSequences) only(CharSequences)
doc { doc {