Skip to content

Instantly share code, notes, and snippets.

View sajjadyousefnia's full-sized avatar
🤒
Out sick

Sajjad Yousefnia sajjadyousefnia

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