Skip to content

Instantly share code, notes, and snippets.

View tasaquino's full-sized avatar

Thais Aquino tasaquino

View GitHub Profile
@tasaquino
tasaquino / simpleEnumExample.kt
Created March 5, 2018 18:26
Kotlin - Simple Enum Example
enum class House {
BARATHEON,
BOLTON,
LANNISTER,
MARTELL,
REDWYNE,
STARK,
TARGARYEN,
TULLY;
}
@tasaquino
tasaquino / enumWithConstructorExample.kt
Last active March 6, 2018 22:24
Kotlin - Enum with constructor example
enum class House(val words:String) {
BARATHEON("Ours Is the Fury"),
BOLTON("Our Blades Are Sharp"),
LANNISTER("A Lannister Always Pays His Debts"),
MARTELL("Unbowed, Unbent, Unbroken"),
STARK("Winter Is Coming"),
TARGARYEN("Fire and Blood"),
TULLY("Family, Duty, Honor");
}
@tasaquino
tasaquino / enumWithFunction.kt
Last active March 6, 2018 22:30
Kotlin - Enum with functions Example
enum class House(val words:String) {
BARATHEON("Ours Is the Fury"),
BOLTON("Our Blades Are Sharp"),
LANNISTER("A Lannister Always Pays His Debts"),
MARTELL("Unbowed, Unbent, Unbroken"),
STARK("Winter Is Coming"),
TARGARYEN("Fire and Blood"),
TULLY("Family, Duty, Honor");
fun sayWords() = "Words: $words"
@tasaquino
tasaquino / enumInheritFromInterface.kt
Created March 7, 2018 00:19
Kotlin - Enum inherit from interface example
interface SaySomething {
fun say(): String
}
enum class House : SaySomething {
BARATHEON {
override fun say() = "Ours Is the Fury"
},
BOLTON {
override fun say() = "Our Blades Are Sharp"
@tasaquino
tasaquino / whenWithEnum.kt
Created March 7, 2018 00:30
Kotlin - When with enum example
fun showWordsOfHouse(house: House) =
when (house) {
House.BARATHEON -> "Ours Is the Fury"
House.BOLTON -> "Our Blades Are Sharp"
House.LANNISTER -> "A Lannister Always Pays His Debts"
House.MARTELL -> "Unbowed, Unbent, Unbroken"
House.STARK -> "Winter Is Coming"
House.TARGARYEN -> "Fire and Blood"
House.TULLY -> "Family, Duty, Honor"
}
@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
}
@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 / 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 / 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 / ScopingFunLetExample.kt
Last active November 30, 2018 14:43
Scoping Functions - let example
val person: Person = attributes.let {
mapToPerson(it.first, it.second)
}