Make more generic signature for reduce and reduceRight methods.

Fixes #KT-2287
This commit is contained in:
Ilya Gorbunov
2015-04-10 20:27:34 +03:00
parent 08e92ac746
commit 043b27bfe5
3 changed files with 106 additions and 61 deletions
+58 -58
View File
@@ -2122,10 +2122,51 @@ public inline fun String.none(predicate: (Char) -> Boolean): Boolean {
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <T> Array<out T>.reduce(operation: (T, T) -> T): T {
public inline fun <S, T: S> Array<out T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = iterator.next()
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <S, T: S> Sequence<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <S, T: S> Stream<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
@@ -2236,47 +2277,6 @@ public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short {
return accumulator
}
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <T> Iterable<T>.reduce(operation: (T, T) -> T): T {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <T> Sequence<T>.reduce(operation: (T, T) -> T): T {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <T> Stream<T>.reduce(operation: (T, T) -> T): T {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
@@ -2293,10 +2293,23 @@ public inline fun String.reduce(operation: (Char, Char) -> Char): Char {
/**
* Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value
*/
public inline fun <T> Array<out T>.reduceRight(operation: (T, T) -> T): T {
public inline fun <S, T: S> Array<out T>.reduceRight(operation: (T, S) -> S): S {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = get(index--)
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
}
return accumulator
}
/**
* Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value
*/
public inline fun <S, T: S> List<T>.reduceRight(operation: (T, S) -> S): S {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
}
@@ -2407,19 +2420,6 @@ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Sh
return accumulator
}
/**
* Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value
*/
public inline fun <T> List<T>.reduceRight(operation: (T, T) -> T): T {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
}
return accumulator
}
/**
* Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value
*/
@@ -151,7 +151,7 @@ class CollectionTest {
// TODO replace with more accurate version when KT-5987 will be fixed
// failsWith(javaClass<UnsupportedOperationException>()) {
fails {
arrayListOf<Int>().reduce { a, b -> a + b }
run { arrayListOf<Int>().reduce { a, b -> a + b } }
}
}
@@ -164,7 +164,7 @@ class CollectionTest {
// TODO replace with more accurate version when KT-5987 will be fixed
// failsWith(javaClass<UnsupportedOperationException>()) {
fails {
arrayListOf<Int>().reduceRight { a, b -> a + b }
run { arrayListOf<Int>().reduceRight { a, b -> a + b } }
}
}
@@ -378,6 +378,7 @@ fun aggregates(): List<GenericFunction> {
templates add f("reduce(operation: (T, T) -> T)") {
inline(true)
exclude(ArraysOfObjects, Iterables, Sequences)
doc { "Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element" }
returns("T")
@@ -395,10 +396,32 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("reduce(operation: (S, T) -> S)") {
inline(true)
only(ArraysOfObjects, Iterables, Sequences)
doc { "Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element" }
typeParam("S")
typeParam("T: S")
returns("S")
body {
"""
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
"""
}
}
templates add f("reduceRight(operation: (T, T) -> T)") {
inline(true)
only(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives)
only(Strings, ArraysOfPrimitives)
doc { "Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value" }
returns("T")
body {
@@ -416,6 +439,28 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("reduceRight(operation: (T, S) -> S)") {
inline(true)
only(Lists, ArraysOfObjects)
doc { "Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value" }
typeParam("S")
typeParam("T: S")
returns("S")
body {
"""
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
}
return accumulator
"""
}
}
templates add f("forEach(operation: (T) -> Unit)") {
inline(true)