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
person.also { p -> | |
p.profession = "Iron Man" | |
p.nickname = "Tony" | |
} |
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
person.also { | |
it.profession = "Iron Man" | |
it.nickname = "Tony" | |
} |
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
person.apply { | |
this.profession = "Iron Man" | |
this.nickname = "Tony" | |
} |
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 person: Person = with(attributes) { | |
mapToPerson(this.first, this.second) | |
} |
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 person: Person = attributes.run { | |
mapToPerson(this.first, this.second) | |
} |
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 person: Person = attributes.let { | |
mapToPerson(it.first, it.second) | |
} |
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 printAnimalsOf(house: House) = | |
when (house) { | |
is Stark -> { | |
val wolves = house.wolves() | |
println(wolves) | |
} | |
is Targeryen -> { | |
val dragons = house.daenerysDragons() | |
println(dragons) | |
} |
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 House | |
class Stark : House{ | |
fun wolves() = arrayListOf("Ghost", "Grey Wind", "Summer", "Lady", "ShaggyDog", "Nymeria") | |
} | |
class Targeryen : House{ | |
fun daenerysDragons() = arrayListOf("Drogon", "Viserion", "Rhaegal") | |
} |
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
enum class Color { | |
RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET | |
} | |
fun mix(firstColor: Color, secondColor: Color) = | |
when (setOf(firstColor, secondColor)) { | |
setOf(Color.RED, Color.YELLOW) -> Color.ORANGE | |
setOf(Color.YELLOW, Color.BLUE) -> Color.GREEN | |
setOf(Color.BLUE, Color.VIOLET) -> Color.INDIGO | |
else -> throw Exception("bla color") |
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 showIsGreatHouse(house: House) = | |
when (house) { | |
House.TARGARYEN, House.LANNISTER, House.STARK -> "$house is a great house" | |
else -> "bla" | |
} | |
fun main(args: Array<String>) { | |
println(showIsGreatHouse(House.LANNISTER)) | |
// LANNISTER is a great house | |
} |