KT-20357: Add sample for reduce, reduceRight and their indexed counterparts

This commit is contained in:
Tillmann Berg
2019-12-07 00:08:24 +01:00
committed by Ilya Gorbunov
parent 6d092b5f71
commit 95300ae31d
7 changed files with 150 additions and 0 deletions
@@ -642,6 +642,24 @@ class Collections {
assertPrints(emptyMin, "null")
}
@Sample
fun reduce() {
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.reduce { acc, string -> acc + string }, "abcd")
assertPrints(strings.reduceIndexed { index, acc, string -> acc + string + index }, "ab1c2d3")
assertFails { emptyList<Int>().reduce { _, _ -> 0 } }
}
@Sample
fun reduceRight() {
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.reduceRight { string, acc -> acc + string }, "dcba")
assertPrints(strings.reduceRightIndexed { index, string, acc -> acc + string + index }, "dc2b1a0")
assertFails { emptyList<Int>().reduceRight { _, _ -> 0 } }
}
@Sample
fun reduceOrNull() {
val strings = listOf("a", "b", "c", "d")