KT-20357 Add samples for maxBy and minBy

This commit is contained in:
Karen Schwane
2018-10-31 08:21:30 +01:00
committed by ilya-g
parent aae24c993d
commit d88fd8aa6f
7 changed files with 76 additions and 0 deletions
@@ -497,6 +497,28 @@ class Collections {
val emptyList = emptyList<Int>()
assertFalse(emptyList.any { true })
}
@Sample
fun maxBy() {
val nameToAge = listOf("Alice" to 42, "Bob" to 28, "Carol" to 51)
val oldestPerson = nameToAge.maxBy { it.second }
assertPrints(oldestPerson, "(Carol, 51)")
val emptyList = emptyList<Pair<String, Int>>()
val emptyMax = emptyList.maxBy { it.second }
assertPrints(emptyMax, "null")
}
@Sample
fun minBy() {
val list = listOf("abcd", "abc", "ab", "abcde")
val shortestString = list.minBy { it.length }
assertPrints(shortestString, "ab")
val emptyList = emptyList<String>()
val emptyMin = emptyList.minBy { it.length }
assertPrints(emptyMin, "null")
}
}
class Elements {