Zhuinden / guide-to-kotlin

[GUIDE] This tutorial assumes all you know is Java, but you want to learn Kotlin.
https://github.com/Zhuinden/guide-to-kotlin/wiki
Apache License 2.0
1.29k stars 99 forks source link

Collections initializers #10

Closed jmfayard closed 5 years ago

jmfayard commented 5 years ago

Re: https://github.com/Zhuinden/guide-to-kotlin/wiki/1.)-Major-Syntax-Differences#arrayof-and-arrayofnulls-and-listof-and-mutablelistof-and-linkedmapof

There is a handy collection initializer that is not very well-known where you create a list with a size of elements and a lambda for each element

val squares = List(size = 6) { it * it } 
// same as:
val squares = listOf(0, 1, 4, 9, 16, 25)
Zhuinden commented 5 years ago

Added, thanks

jmfayard commented 5 years ago

@Zhuinden It was fun to review this first chapter and I could do that for the others when I have time. Is there a better way to contribute than via the issues?

Zhuinden commented 5 years ago

Technically I could be like Codepath and just untick this box:

[X] Restrict editing to collaborators only Public wikis will still be readable by everyone.

Or I could invite you to be a collaborator, which is what I did :+1: