This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val list1: List<String> = listOf("a", "b") | |
val list2: List<Int> = listOf(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (element in this) { | |
if (element is String) { | |
destination.add(element) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun hasZeros(ints: List<Int>): Boolean { | |
ints.forEach { | |
if (it == 0) return true // returns from hasZeros | |
} | |
return false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> | |
Iterable<*>.filterIsInstance(): List<T> | |
val destination = mutableListOf<T>() | |
for (element in this) { | |
if (element is T) { | |
destination.add(element) | |
} | |
} | |
return destination | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> val items = listOf("one", 2, "three") | |
>>> println(items.filterIsInstance<String>()) | |
[one, three] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> isA(value: Any) = value is T | |
>>> println(isA<String>("abc")) | |
true | |
>>> println(isA<String>(123)) | |
false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> isA(value: Any) = value is T | |
>>> println(isA<String>("abc")) | |
true | |
>>> println(isA<String>(123)) | |
false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> fun <T> isA(value: Any) = value is T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T : Number> oneHalf(value: T): Double { | |
return value.toDouble() / 2.0 | |
} | |
>>> println(oneHalf(3)) | |
1.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface List<T> { | |
operator fun get(index: Int): T | |
// ... | |
} |