Introduce firstNotNullOf and firstNotNullOfOrNull #KT-12109

This commit is contained in:
Abduqodiri Qurbonzoda
2021-03-11 18:08:26 +03:00
parent d4b3ae92cb
commit ff5b2404af
15 changed files with 342 additions and 0 deletions
@@ -436,6 +436,22 @@ class MapTest {
doTest("built Map", builtMap, "y", 24)
}
@Test
fun firstNotNullOf() {
val map = mapOf("Alice" to 20, "Tom" to 13, "Bob" to 18)
val firstAdult = map.firstNotNullOf { (name, age) -> name.takeIf { age >= 18 } }
val firstAdultOrNull = map.firstNotNullOfOrNull { (name, age) -> name.takeIf { age >= 18 } }
assertEquals("Alice", firstAdult)
assertEquals("Alice", firstAdultOrNull)
assertFailsWith<NoSuchElementException> { val firstChild = map.firstNotNullOf { (name, age) -> name.takeIf { age <= 11 } } }
val firstChildOrNull = map.firstNotNullOfOrNull { (name, age) -> name.takeIf { age <= 11 } }
assertNull(firstChildOrNull)
}
fun testPlusAssign(doPlusAssign: (MutableMap<String, Int>) -> Unit) {
val map = hashMapOf("a" to 1, "b" to 2)