This file contains hidden or 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 message = { myString: String -> println(myString) } | |
message("I love Kotlin") // "I love Kotlin" | |
message("How far?") // "How far?" |
This file contains hidden or 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 message = { myString -> println(myString) } // will still compile |
This file contains hidden or 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 addNumbers = { number1: Int, number2: Int -> | |
println("Adding $number1 and $number2") | |
val result = number1 + number2 | |
println("The result is $result") | |
} | |
addNumbers(1, 3) |
This file contains hidden or 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
Adding 1 and 3 | |
The result is 4 |
This file contains hidden or 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 stringList: List<String> = listOf("in", "the", "club") | |
print(stringList.last()) // will print "club" | |
print(stringList.last({ s: String -> s.length == 3})) // will print "the" |
This file contains hidden or 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
stringList.last { s: String -> s.length == 3 } // will also compile and print "the" |
This file contains hidden or 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
stringList.last { s -> s.length == 3 } // will also compile print "the" |
This file contains hidden or 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
stringList.last { it.length == 3 } |
This file contains hidden or 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 surroundingFunction() { | |
val intList = listOf(1, 2, 3, 4, 5) | |
intList.forEach { | |
if (it % 2 == 0) { | |
return | |
} | |
} | |
println("End of surroundingFunction()") | |
} | |
This file contains hidden or 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
// ... | |
println("End of surroundingFunction()") // This won't execute | |
// ... |