Generalize zip(..) to merge(..) { t1, t2 -> ... }, add merge for maps.
This commit is contained in:
committed by
Andrey Breslav
parent
f94b79fac3
commit
65da4cb2fb
@@ -7,6 +7,299 @@ package kotlin
|
|||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <T, R, V> Array<out T>.merge(array: Array<R>, transform: (T, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> BooleanArray.merge(array: Array<R>, transform: (Boolean, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> ByteArray.merge(array: Array<R>, transform: (Byte, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> CharArray.merge(array: Array<R>, transform: (Char, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> DoubleArray.merge(array: Array<R>, transform: (Double, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> FloatArray.merge(array: Array<R>, transform: (Float, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> IntArray.merge(array: Array<R>, transform: (Int, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> LongArray.merge(array: Array<R>, transform: (Long, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> ShortArray.merge(array: Array<R>, transform: (Short, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <T, R, V> Iterable<T>.merge(array: Array<R>, transform: (T, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> String.merge(array: Array<R>, transform: (Char, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <T, R, V> Array<out T>.merge(other: Iterable<R>, transform: (T, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> BooleanArray.merge(other: Iterable<R>, transform: (Boolean, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> ByteArray.merge(other: Iterable<R>, transform: (Byte, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> CharArray.merge(other: Iterable<R>, transform: (Char, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> DoubleArray.merge(other: Iterable<R>, transform: (Double, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> FloatArray.merge(other: Iterable<R>, transform: (Float, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> IntArray.merge(other: Iterable<R>, transform: (Int, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> LongArray.merge(other: Iterable<R>, transform: (Long, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> ShortArray.merge(other: Iterable<R>, transform: (Short, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <T, R, V> Iterable<T>.merge(other: Iterable<R>, transform: (T, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
*/
|
||||||
|
public inline fun <R, V> String.merge(other: Iterable<R>, transform: (Char, R) -> V): List<V> {
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a stream of values built from elements of both collections with same indexes using provided *transform*. Stream has length of shortest stream.
|
||||||
|
*/
|
||||||
|
public fun <T, R, V> Stream<T>.merge(stream: Stream<R>, transform: (T, R) -> V): Stream<V> {
|
||||||
|
return MergingStream(this, stream, transform)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits original collection into pair of collections,
|
* Splits original collection into pair of collections,
|
||||||
* where *first* collection contains elements for which predicate yielded *true*,
|
* where *first* collection contains elements for which predicate yielded *true*,
|
||||||
@@ -518,286 +811,154 @@ public fun <T> Stream<T>.plus(stream: Stream<T>): Stream<T> {
|
|||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <T, R> Array<out T>.zip(array: Array<R>): List<Pair<T, R>> {
|
public fun <T, R> Array<out T>.zip(array: Array<R>): List<Pair<T, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<T, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> BooleanArray.zip(array: Array<R>): List<Pair<Boolean, R>> {
|
public fun <R> BooleanArray.zip(array: Array<R>): List<Pair<Boolean, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Boolean, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> ByteArray.zip(array: Array<R>): List<Pair<Byte, R>> {
|
public fun <R> ByteArray.zip(array: Array<R>): List<Pair<Byte, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Byte, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> CharArray.zip(array: Array<R>): List<Pair<Char, R>> {
|
public fun <R> CharArray.zip(array: Array<R>): List<Pair<Char, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Char, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> DoubleArray.zip(array: Array<R>): List<Pair<Double, R>> {
|
public fun <R> DoubleArray.zip(array: Array<R>): List<Pair<Double, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Double, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> FloatArray.zip(array: Array<R>): List<Pair<Float, R>> {
|
public fun <R> FloatArray.zip(array: Array<R>): List<Pair<Float, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Float, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> IntArray.zip(array: Array<R>): List<Pair<Int, R>> {
|
public fun <R> IntArray.zip(array: Array<R>): List<Pair<Int, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Int, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> LongArray.zip(array: Array<R>): List<Pair<Long, R>> {
|
public fun <R> LongArray.zip(array: Array<R>): List<Pair<Long, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Long, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> ShortArray.zip(array: Array<R>): List<Pair<Short, R>> {
|
public fun <R> ShortArray.zip(array: Array<R>): List<Pair<Short, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Short, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <T, R> Iterable<T>.zip(array: Array<R>): List<Pair<T, R>> {
|
public fun <T, R> Iterable<T>.zip(array: Array<R>): List<Pair<T, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<T, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> String.zip(array: Array<R>): List<Pair<Char, R>> {
|
public fun <R> String.zip(array: Array<R>): List<Pair<Char, R>> {
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<Char, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <T, R> Array<out T>.zip(other: Iterable<R>): List<Pair<T, R>> {
|
public fun <T, R> Array<out T>.zip(other: Iterable<R>): List<Pair<T, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<T, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> BooleanArray.zip(other: Iterable<R>): List<Pair<Boolean, R>> {
|
public fun <R> BooleanArray.zip(other: Iterable<R>): List<Pair<Boolean, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Boolean, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> ByteArray.zip(other: Iterable<R>): List<Pair<Byte, R>> {
|
public fun <R> ByteArray.zip(other: Iterable<R>): List<Pair<Byte, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Byte, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> CharArray.zip(other: Iterable<R>): List<Pair<Char, R>> {
|
public fun <R> CharArray.zip(other: Iterable<R>): List<Pair<Char, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Char, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> DoubleArray.zip(other: Iterable<R>): List<Pair<Double, R>> {
|
public fun <R> DoubleArray.zip(other: Iterable<R>): List<Pair<Double, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Double, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> FloatArray.zip(other: Iterable<R>): List<Pair<Float, R>> {
|
public fun <R> FloatArray.zip(other: Iterable<R>): List<Pair<Float, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Float, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> IntArray.zip(other: Iterable<R>): List<Pair<Int, R>> {
|
public fun <R> IntArray.zip(other: Iterable<R>): List<Pair<Int, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Int, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> LongArray.zip(other: Iterable<R>): List<Pair<Long, R>> {
|
public fun <R> LongArray.zip(other: Iterable<R>): List<Pair<Long, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Long, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> ShortArray.zip(other: Iterable<R>): List<Pair<Short, R>> {
|
public fun <R> ShortArray.zip(other: Iterable<R>): List<Pair<Short, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Short, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T, R>> {
|
public fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<T, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||||
*/
|
*/
|
||||||
public fun <R> String.zip(other: Iterable<R>): List<Pair<Char, R>> {
|
public fun <R> String.zip(other: Iterable<R>): List<Pair<Char, R>> {
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<Char, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -814,9 +975,9 @@ public fun String.zip(other: String): List<Pair<Char, Char>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a stream of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
* Returns a stream of pairs built from elements of both collections with same indexes. Stream has length of shortest stream.
|
||||||
*/
|
*/
|
||||||
public fun <T, R> Stream<T>.zip(stream: Stream<R>): Stream<Pair<T, R>> {
|
public fun <T, R> Stream<T>.zip(stream: Stream<R>): Stream<Pair<T, R>> {
|
||||||
return ZippingStream(this, stream)
|
return MergingStream(this, stream) { (t1, t2) -> t1 to t2 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,13 +38,13 @@ public class TransformingStream<T, R>(val stream: Stream<T>, val transformer: (T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZippingStream<T1, T2>(val stream1: Stream<T1>, val stream2: Stream<T2>) : Stream<Pair<T1, T2>> {
|
public class MergingStream<T1, T2, V>(val stream1: Stream<T1>, val stream2: Stream<T2>, val transform: (T1, T2) -> V) : Stream<V> {
|
||||||
override fun iterator(): Iterator<Pair<T1, T2>> = object : AbstractIterator<Pair<T1, T2>>() {
|
override fun iterator(): Iterator<V> = object : AbstractIterator<V>() {
|
||||||
val iterator1 = stream1.iterator()
|
val iterator1 = stream1.iterator()
|
||||||
val iterator2 = stream2.iterator()
|
val iterator2 = stream2.iterator()
|
||||||
override fun computeNext() {
|
override fun computeNext() {
|
||||||
if (iterator1.hasNext() && iterator2.hasNext()) {
|
if (iterator1.hasNext() && iterator2.hasNext()) {
|
||||||
setNext(iterator1.next() to iterator2.next())
|
setNext(transform(iterator1.next(), iterator2.next()))
|
||||||
} else {
|
} else {
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,6 +120,20 @@ class CollectionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test
|
||||||
|
fun merge() {
|
||||||
|
expect(listOf("ab", "bc", "cd")) {
|
||||||
|
listOf("a", "b", "c").merge(listOf("b", "c", "d")) { a, b -> a + b }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test
|
||||||
|
fun zip() {
|
||||||
|
expect(listOf("a" to "b", "b" to "c", "c" to "d")) {
|
||||||
|
listOf("a", "b", "c").zip(listOf("b", "c", "d"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test fun partition() {
|
test fun partition() {
|
||||||
val data = arrayListOf("foo", "bar", "something", "xyz")
|
val data = arrayListOf("foo", "bar", "something", "xyz")
|
||||||
val pair = data.partition { it.size == 3 }
|
val pair = data.partition { it.size == 3 }
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class MapTest {
|
|||||||
assertEquals(map.size(), 1)
|
assertEquals(map.size(), 1)
|
||||||
assertEquals("James", map["name"])
|
assertEquals("James", map["name"])
|
||||||
}
|
}
|
||||||
|
|
||||||
test fun iterate() {
|
test fun iterate() {
|
||||||
val map = TreeMap<String, String>()
|
val map = TreeMap<String, String>()
|
||||||
map["beverage"] = "beer"
|
map["beverage"] = "beer"
|
||||||
|
|||||||
@@ -73,6 +73,13 @@ public class StreamTest {
|
|||||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(3).drop(4).joinToString(limit = 10))
|
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(3).drop(4).joinToString(limit = 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test
|
||||||
|
fun merge() {
|
||||||
|
expect(listOf("ab", "bc", "cd")) {
|
||||||
|
streamOf("a", "b", "c").merge(streamOf("b", "c", "d")) { a, b -> a + b }.toList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test fun toStringJoinsNoMoreThanTheFirstTenElements() {
|
test fun toStringJoinsNoMoreThanTheFirstTenElements() {
|
||||||
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().joinToString(limit = 10))
|
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().joinToString(limit = 10))
|
||||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.joinToString(limit = 10))
|
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.joinToString(limit = 10))
|
||||||
|
|||||||
@@ -118,6 +118,73 @@ fun generators(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templates add f("merge(other: Iterable<R>, transform: (T, R) -> V)") {
|
||||||
|
exclude(Streams)
|
||||||
|
doc {
|
||||||
|
"""
|
||||||
|
Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
typeParam("R")
|
||||||
|
typeParam("V")
|
||||||
|
returns("List<V>")
|
||||||
|
inline(true)
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
val first = iterator()
|
||||||
|
val second = other.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
templates add f("merge(array: Array<R>, transform: (T, R) -> V)") {
|
||||||
|
exclude(Streams)
|
||||||
|
doc {
|
||||||
|
"""
|
||||||
|
Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
typeParam("R")
|
||||||
|
typeParam("V")
|
||||||
|
returns("List<V>")
|
||||||
|
inline(true)
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
val first = iterator()
|
||||||
|
val second = array.iterator()
|
||||||
|
val list = arrayListOf<V>()
|
||||||
|
while (first.hasNext() && second.hasNext()) {
|
||||||
|
list.add(transform(first.next(), second.next()))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
templates add f("merge(stream: Stream<R>, transform: (T, R) -> V)") {
|
||||||
|
only(Streams)
|
||||||
|
doc {
|
||||||
|
"""
|
||||||
|
Returns a stream of values built from elements of both collections with same indexes using provided *transform*. Stream has length of shortest stream.
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
typeParam("R")
|
||||||
|
typeParam("V")
|
||||||
|
returns("Stream<V>")
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
return MergingStream(this, stream, transform)
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
templates add f("zip(other: Iterable<R>)") {
|
templates add f("zip(other: Iterable<R>)") {
|
||||||
exclude(Streams)
|
exclude(Streams)
|
||||||
doc {
|
doc {
|
||||||
@@ -129,13 +196,7 @@ fun generators(): List<GenericFunction> {
|
|||||||
returns("List<Pair<T, R>>")
|
returns("List<Pair<T, R>>")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
val first = iterator()
|
return merge(other) { (t1, t2) -> t1 to t2 }
|
||||||
val second = other.iterator()
|
|
||||||
val list = ArrayList<Pair<T, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,13 +233,7 @@ fun generators(): List<GenericFunction> {
|
|||||||
returns("List<Pair<T, R>>")
|
returns("List<Pair<T, R>>")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
val first = iterator()
|
return merge(array) { (t1, t2) -> t1 to t2 }
|
||||||
val second = array.iterator()
|
|
||||||
val list = ArrayList<Pair<T, R>>()
|
|
||||||
while (first.hasNext() && second.hasNext()) {
|
|
||||||
list.add(first.next() to second.next())
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,14 +242,14 @@ fun generators(): List<GenericFunction> {
|
|||||||
only(Streams)
|
only(Streams)
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Returns a stream of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
Returns a stream of pairs built from elements of both collections with same indexes. Stream has length of shortest stream.
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
typeParam("R")
|
typeParam("R")
|
||||||
returns("Stream<Pair<T, R>>")
|
returns("Stream<Pair<T, R>>")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
return ZippingStream(this, stream)
|
return MergingStream(this, stream) { (t1, t2) -> t1 to t2 }
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user