Skip to content

Instantly share code, notes, and snippets.

View tasaquino's full-sized avatar

Thais Aquino tasaquino

View GitHub Profile
@tasaquino
tasaquino / ScopingFunAlso2.kt
Created November 30, 2018 15:19
Scoping Function - also example 2
person.also { p ->
p.profession = "Iron Man"
p.nickname = "Tony"
}
@tasaquino
tasaquino / ScopingFunAlsoExample.kt
Last active November 30, 2018 15:17
Scoping Functions - also example
person.also {
it.profession = "Iron Man"
it.nickname = "Tony"
}
@tasaquino
tasaquino / ScopingFunApplyExample.kt
Last active November 30, 2018 15:04
Scoping Functions - apply example
person.apply {
this.profession = "Iron Man"
this.nickname = "Tony"
}
@tasaquino
tasaquino / ScopingFunWithExample.kt
Created November 30, 2018 14:49
Scoping Functions - with example
val person: Person = with(attributes) {
mapToPerson(this.first, this.second)
}
@tasaquino
tasaquino / ScopingFunRunExample.kt
Created November 30, 2018 14:46
Scoping Functions - run example
val person: Person = attributes.run {
mapToPerson(this.first, this.second)
}
@tasaquino
tasaquino / ScopingFunLetExample.kt
Last active November 30, 2018 14:43
Scoping Functions - let example
val person: Person = attributes.let {
mapToPerson(it.first, it.second)
}
@tasaquino
tasaquino / whenConditionsWithBlocksExample.kt
Created March 7, 2018 14:23
Kotlin - When conditions with blocks
fun printAnimalsOf(house: House) =
when (house) {
is Stark -> {
val wolves = house.wolves()
println(wolves)
}
is Targeryen -> {
val dragons = house.daenerysDragons()
println(dragons)
}
@tasaquino
tasaquino / whenWithSmartCastExample.kt
Last active March 7, 2018 14:08
Kotlin - when with smart cast example
interface House
class Stark : House{
fun wolves() = arrayListOf("Ghost", "Grey Wind", "Summer", "Lady", "ShaggyDog", "Nymeria")
}
class Targeryen : House{
fun daenerysDragons() = arrayListOf("Drogon", "Viserion", "Rhaegal")
}
@tasaquino
tasaquino / anyObjectEnumExample.kt
Created March 7, 2018 12:06
Kotlin - Enum with any object example
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")
@tasaquino
tasaquino / combinedConstantsEnum.kt
Last active March 7, 2018 11:40
Kotlin - Combined Constants Enum Example
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
}