Implement reduce and reduceRight functions

This commit is contained in:
Alexander Zolotov
2012-06-16 00:46:20 +04:00
parent 4657567484
commit 0043454a06
14 changed files with 398 additions and 3 deletions
+31
View File
@@ -216,6 +216,37 @@ class CollectionTest {
}
}
test fun reduce() {
expect("1234") {
val list = arrayList("1", "2", "3", "4")
list.reduce { a, b -> a + b }
}
expect(-4) {
val array = array(1, 2, 3)
array.reduce { a, b -> a - b }
}
failsWith<UnsupportedOperationException> {
arrayList<Int>().reduce { a, b -> a + b}
}
}
test fun reduceRight() {
expect("1234") {
val list = arrayList("1", "2", "3", "4")
list.reduceRight { a, b -> a + b }
}
expect(2) {
val array = array(1, 2, 3)
array.reduceRight { a, b -> a - b }
}
failsWith<UnsupportedOperationException> {
arrayList<Int>().reduceRight { a, b -> a + b}
}
}
test fun groupBy() {
val words = arrayList("a", "ab", "abc", "def", "abcd")
val byLength = words.groupBy{ it.length }