fixes KT-2091 allowing fold() and foldRight() to take different types of initial value and return the same typed result

This commit is contained in:
James Strachan
2012-09-21 16:31:04 +01:00
parent e129b43a7c
commit be44702145
12 changed files with 41 additions and 22 deletions
+2 -2
View File
@@ -142,7 +142,7 @@ public inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) : Unit = for (
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <T> Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
public inline fun <T,R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -153,7 +153,7 @@ public inline fun <T> Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <T> Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <T,R> Iterable<T>.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
/**