Created
June 1, 2018 07:28
-
-
Save ivalexandru/952750a1a071c37256115d8c0859a2dd to your computer and use it in GitHub Desktop.
Kotlin_functii_compacte
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>) { | |
println(shouldChangeWater("Friday")) | |
} | |
fun getDirtySensorReading() = 20 | |
//acum, jos in functia shouldChangeWater inlocuiesc 20-ul cu functia asta, iar cand se va schimba functia asta(valoarea parametrii etc), in functia mare nu mai modific nimic | |
fun shouldChangeWater( | |
day: String, | |
temperature: Int = 22, | |
dirty: Int = getDirtySensorReading() ) : Boolean { | |
//var1 | |
// return when { | |
// temperature > 30 -> true | |
// dirty > 30 -> true | |
// day == "Sunday" -> true | |
// else -> false | |
// } | |
//var2 | |
// val isTooHot = temperature > 30 | |
//// val isDirty = dirty > 30 | |
//// val isSunday = day === "Sunday" | |
//// return when { | |
//// isTooHot > 30 -> true | |
//// dirty > 30 -> true | |
//// day == "Sunday" -> true | |
//// else -> false | |
//// } | |
//var3 | |
fun isTooHot(temperature: Int) :Boolean = temperature > 30 | |
fun isDirty(dirty: Int): Boolean = dirty > 30 | |
fun isSunday(day: String): Boolean = day == "Sunday" | |
return when { | |
isTooHot(temperature) -> true | |
isDirty(dirty) -> true | |
isSunday(day) -> true | |
else -> false | |
} | |
//O CHESTIE FAINA DACA FOLOSESTI VARIANTA3 ESTE ca in loc de valoare fixa ca parametru default, poti sa asignezi o functie, who's return value becomes the default parameter | |
} | |
//EXPENSIVE FUNCTIONS = reading files, allocating alot of memory, SLOWS DOWN YOUR APP, because default parameters are EVALUATED AT CALLTIME | |
//ex: | |
fun makeNewAquarium() = println("Building a new aquarium") | |
fun aquariumStatusReport(aquarium: Any =makeNewAquarium()) {} | |
//when called, you can pass in an existing aquarium, BUT IF YOU DON'T, it builds an entirely new aquarium(si dureaza mult asta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment