Last active
April 23, 2021 12:53
-
-
Save harsh183/0b37207cd79b93037b9d30ca665414d4 to your computer and use it in GitHub Desktop.
Comparing directly declaring lists vs initializer functions (using lambdas). The syntax is pretty decent except for maps but to be fair if you're doing this type of thing you should not be using maps.
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 main() { | |
// Both are the same | |
val array1: Array<Int> = arrayOf(0, 1, 4, 9, 16) | |
val list1: List<Int> = listOf(0, 1, 4, 9, 16) | |
val map1: Map<Int, Int> = mapOf(0 to 0, 1 to 1, 2 to 4, 3 to 9, 4 to 16) | |
val array2: Array<Int> = Array(5) { it*it } | |
val list2: List<Int> = List(5) {it*it} | |
val map2: Map<Int, Int> = (Array(5) {it}).associate {it to it*it} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Kotlin type inference is so good this entire code works without Type declarations.