Fix foldRight implementation for iterables

This commit is contained in:
Alexander Zolotov
2012-06-15 13:22:47 +04:00
parent ef1946eee2
commit ae00b0bb15
15 changed files with 35 additions and 21 deletions
+15 -1
View File
@@ -195,13 +195,27 @@ class CollectionTest {
}
}
test fun foldWithNonCommutativeOperation() {
expect(1) {
val numbers = arrayList(1, 2, 3)
numbers.fold(7) {a, b -> a - b}
}
}
test fun foldRight() {
expect("4321") {
expect("1234") {
val numbers = arrayList(1, 2, 3, 4)
numbers.map<Int, String>{it.toString()}.foldRight(""){ a, b -> a + b}
}
}
test fun foldRightWithNonCommutativeOperation() {
expect(-5) {
val numbers = arrayList(1, 2, 3)
numbers.foldRight(7) {a, b -> a - b}
}
}
test fun groupBy() {
val words = arrayList("a", "ab", "abc", "def", "abcd")
val byLength = words.groupBy{ it.length }