Add samples for zip on sequences and strings

This commit is contained in:
Ilya Gorbunov
2018-04-19 21:16:11 +03:00
parent 2d4e591b5d
commit 0eae27e03f
5 changed files with 48 additions and 0 deletions
@@ -1560,6 +1560,8 @@ public fun <T, R> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows:
* Returns a sequence of pairs built from elements of both sequences with same indexes. * Returns a sequence of pairs built from elements of both sequences with same indexes.
* Resulting sequence has length of shortest input sequence. * Resulting sequence has length of shortest input sequence.
* *
* @sample samples.collections.Sequences.Transformations.zip
*
* The operation is _intermediate_ and _stateless_. * The operation is _intermediate_ and _stateless_.
*/ */
public infix fun <T, R> Sequence<T>.zip(other: Sequence<R>): Sequence<Pair<T, R>> { public infix fun <T, R> Sequence<T>.zip(other: Sequence<R>): Sequence<Pair<T, R>> {
@@ -1569,6 +1571,8 @@ public infix fun <T, R> Sequence<T>.zip(other: Sequence<R>): Sequence<Pair<T, R>
/** /**
* Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences.
* *
* @sample samples.collections.Sequences.Transformations.zipWithTransform
*
* The operation is _intermediate_ and _stateless_. * The operation is _intermediate_ and _stateless_.
*/ */
public fun <T, R, V> Sequence<T>.zip(other: Sequence<R>, transform: (a: T, b: R) -> V): Sequence<V> { public fun <T, R, V> Sequence<T>.zip(other: Sequence<R>, transform: (a: T, b: R) -> V): Sequence<V> {
@@ -1313,6 +1313,8 @@ public fun <R> CharSequence.windowedSequence(size: Int, step: Int = 1, partialWi
/** /**
* 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.
*
* @sample samples.text.Strings.zip
*/ */
public infix fun CharSequence.zip(other: CharSequence): 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 }
@@ -1320,6 +1322,8 @@ public infix fun CharSequence.zip(other: CharSequence): List<Pair<Char, Char>> {
/** /**
* 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.
*
* @sample samples.text.Strings.zipWithTransform
*/ */
public inline fun <V> CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List<V> { public inline fun <V> CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List<V> {
val length = minOf(this.length, other.length) val length = minOf(this.length, other.length)
@@ -188,6 +188,23 @@ class Sequences {
val averagedNoPartialWindows = dataPoints.windowed(size = 4, step = 1).map { it.average() } val averagedNoPartialWindows = dataPoints.windowed(size = 4, step = 1).map { it.average() }
assertPrints(averagedNoPartialWindows.toList(), "[17.0, 19.25, 20.75, 19.75, 15.5, 12.0]") assertPrints(averagedNoPartialWindows.toList(), "[17.0, 19.25, 20.75, 19.75, 15.5, 12.0]")
} }
@Sample
fun zip() {
val sequenceA = ('a'..'z').asSequence()
val sequenceB = generateSequence(1) { it * 2 + 1 }
assertPrints((sequenceA zip sequenceB).take(4).toList(), "[(a, 1), (b, 3), (c, 7), (d, 15)]")
}
@Sample
fun zipWithTransform() {
val sequenceA = ('a'..'z').asSequence()
val sequenceB = generateSequence(1) { it * 2 + 1 }
val result = sequenceA.zip(sequenceB) { a, b -> "$a/$b" }
assertPrints(result.take(4).toList(), "[a/1, b/3, c/7, d/15]")
}
} }
} }
@@ -71,6 +71,21 @@ class Strings {
assertPrints(proteins.take(5).toList(), "[Isoleucine, Arginine, Glycine, Arginine, Glutamine]") assertPrints(proteins.take(5).toList(), "[Isoleucine, Arginine, Glycine, Arginine, Glutamine]")
} }
@Sample
fun zip() {
val stringA = "abcd"
val stringB = "zyx"
assertPrints(stringA zip stringB, "[(a, z), (b, y), (c, x)]")
}
@Sample
fun zipWithTransform() {
val stringA = "abcd"
val stringB = "zyx"
val result = stringA.zip(stringB) { a, b -> "$a$b" }
assertPrints(result, "[az, by, cx]")
}
@Sample @Sample
fun stringToByteArray() { fun stringToByteArray() {
val charset = Charsets.UTF_8 val charset = Charsets.UTF_8
@@ -1108,6 +1108,8 @@ object Generators : TemplateGroupBase() {
doc { doc {
""" """
Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences.
@sample samples.collections.Sequences.Transformations.zipWithTransform
""" """
} }
sequenceClassification(intermediate, stateless) sequenceClassification(intermediate, stateless)
@@ -1127,6 +1129,8 @@ object Generators : TemplateGroupBase() {
doc { doc {
""" """
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.
@sample samples.text.Strings.zipWithTransform
""" """
} }
typeParam("V") typeParam("V")
@@ -1173,6 +1177,8 @@ object Generators : TemplateGroupBase() {
doc { doc {
""" """
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.
@sample samples.text.Strings.zip
""" """
} }
returns("List<Pair<Char, Char>>") returns("List<Pair<Char, Char>>")
@@ -1230,6 +1236,8 @@ object Generators : TemplateGroupBase() {
""" """
Returns a sequence of pairs built from elements of both sequences with same indexes. Returns a sequence of pairs built from elements of both sequences with same indexes.
Resulting sequence has length of shortest input sequence. Resulting sequence has length of shortest input sequence.
@sample samples.collections.Sequences.Transformations.zip
""" """
} }
sequenceClassification(intermediate, stateless) sequenceClassification(intermediate, stateless)