Created
August 16, 2016 02:51
-
-
Save davenportw15/da775b7bbe7d0ae3d888a9781f30a933 to your computer and use it in GitHub Desktop.
Demonstration of kotlin
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(args: Array<String>) | |
{ | |
val users = listOf( | |
User(username = "will", isAdmin = true), | |
User(username = "james", isAdmin = true), | |
User(username = "aneesh", isAdmin = true), | |
User(username = "usain"), | |
User(username = "michael") | |
) | |
val admins = users.filter { user -> user.isAdmin } | |
val adminPairs = admins | |
.flatMap { admin -> admins.map { Pair(admin, it) } } | |
.filterNot { pair -> pair.first == pair.second } | |
adminPairs.forEach { pair -> | |
pair.first.greet(pair.second) | |
} | |
} | |
data class User(val username: String, val isAdmin: Boolean) | |
{ | |
constructor(username: String) : this(username, isAdmin = false) | |
fun greet(other: User) | |
{ | |
println("Hello ${other.username}, I am ${this.username}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment