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
fun surroundingFunction() {
val intList = listOf(1, 2, 3, 4, 5)
intList.forEach {
if (it % 2 == 0) {
return@forEach
}
}
println("End of surroundingFunction()") // Now, it will execute
}
// ...
intList.forEach myLabel@ {
if (it % 2 == 0) {
return@myLabel
// ...
fun surroundingFunction() {
val intList = listOf(1, 2, 3, 4, 5)
intList.forEach {
if (it % 2 == 0) {
return@forEach
}
}
println("End of surroundingFunction()") // Now, it will execute
}
// ...
intList.forEach myLabel@ {
if (it % 2 == 0) {
return@myLabel
// ...
class Circle {
fun calculateArea(radius: Double): Double {
require(radius > 0, { "Radius must be greater than 0" })
return Math.PI * Math.pow(radius, 2.0)
}
}
val circle = Circle()
print(circle.calculateArea(4.5)) // will print "63.61725123519331"
val stringList: List<String> = listOf("in", "the", "club")
print(stringList.last{ it.length == 3}) // will print "the"
val strLenThree = stringList.last( fun(string): Boolean {
return string.length == 3
})
print(strLenThree) // will print "the"
fun surroundingFunction() {
val intList = listOf(1, 2, 3, 4, 5)
intList.forEach ( fun(number) {
if (number % 2 == 0) {
return
}
})
println("End of surroundingFunction()") // statement executed
}
fun printCircumferenceAndArea(radius: Double): Unit {
fun calCircumference(radius: Double): Double = (2 * Math.PI) * radius
val circumference = "%.2f".format(calCircumference(radius))
fun calArea(radius: Double): Double = (Math.PI) * Math.pow(radius, 2.0)
val area = "%.2f".format(calArea(radius))
print("The circle circumference of $radius radius is $circumference and area is $area")
}