Answers for "kotlin also"

4

kotlin also

| Function | Object reference | Return value   | Is extension function                        |
|----------|------------------|----------------|----------------------------------------------|
| let      | it               | Lambda result  | Yes                                          |
| run      | this             | Lambda result  | Yes                                          |
| run      | -                | Lambda result  | No: called without the context object        |
| with     | this             | Lambda result  | No: takes the context object as an argument. |
| apply    | this             | Context object | Yes                                          |
| also     | it               | Context object | Yes                                          |
Posted by: Guest on January-04-2021
-2

kotlin

import java.util.*    // required import

fun randomDay() : String {
    val week = arrayOf ("Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday")
    return week[Random().nextInt(week.size)]
}

fun fishFood (day : String) : String {
    return when (day) {
        "Monday" -> "flakes"
        "Wednesday" -> "redworms"
        "Thursday" -> "granules"
        "Friday" -> "mosquitoes"
        "Sunday" -> "plankton"
        else -> "nothing"
    }
}

fun feedTheFish() {
    val day = randomDay()
    val food = fishFood(day)
    println ("Today is $day and the fish eat $food")
}

fun main(args: Array<String>) {
    feedTheFish()
}
Posted by: Guest on June-05-2020

Browse Popular Code Answers by Language