Created
September 17, 2021 05:36
-
-
Save ghasemdev/81f0feb882699971a18a8d9ee7d77876 to your computer and use it in GitHub Desktop.
Kotlin Functions (Extension Function, Infix Function, Inline Function, Interface Function, Scope Function, Tail Recursion)
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() { | |
println("asdf".isDigits) | |
println("asdf1234".isDigits) | |
println("123456".isDigits) | |
val content: String? = null | |
println(content.orEmpty()) | |
String.print("hello") | |
} | |
// extension value | |
val String.isDigits: Boolean get() = this.matches(Regex("^[0-9]*$")) | |
// extension function | |
fun <T> T?.orDefault(default: T): T { | |
return this ?: default | |
} | |
// extension companion object | |
fun String.Companion.print(value: String) { | |
println("$$ $value") | |
} |
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() { | |
// val decimal1 = Decimal(5, 7) | |
// val decimal2 = Decimal(6, 7) | |
// println(decimal1 + decimal2) | |
println("hello " * 5) | |
} | |
operator fun String.times(i: Int): String { | |
return buildString { | |
repeat(i) { | |
append(this@times) | |
} | |
} | |
} | |
class Decimal(private val fraction: Int, private val denomination: Int) { | |
infix operator fun plus(other: Decimal): Decimal { | |
return if (this.denomination == other.denomination) { | |
Decimal(this.fraction + other.fraction, this.denomination) | |
} else { | |
val fraction = (other.denomination * this.fraction) + (this.denomination * other.fraction) | |
val denomination = this.denomination * other.denomination | |
Decimal(fraction, denomination) | |
} | |
} | |
override fun toString(): String { | |
return "$fraction/$denomination => ${fraction.toDouble() / denomination}" | |
} | |
} |
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() { | |
doSomething { | |
println("doSomething") | |
} | |
/* println("before") | |
println("doSomething") | |
println("after")*/ | |
} | |
inline fun doSomething(block: () -> Unit) { | |
println("before") | |
block() | |
println("after") | |
} |
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 interface SetOnClickListener { | |
fun click() | |
} | |
fun main() { | |
// val clicked = object : SetOnClickListener { | |
// override fun click() { | |
// println("click") | |
// } | |
// } | |
val clicked = SetOnClickListener { println("click") } | |
clicked.click() | |
} |
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() { | |
// apply | |
val car = Car().apply { | |
color = "black" | |
} | |
// also | |
val car2 = Car().also { car -> | |
car.color | |
} | |
var a = 5 | |
var b = 6 | |
a = b.also { b = a } | |
// let | |
val text: String? = "" | |
val content = text?.let { "i am not null" }.orDefault("i am null") | |
// println(content) | |
// with | |
with(car) { | |
println(color) | |
} | |
// run | |
car.run { | |
drive() | |
} | |
} | |
class Car { | |
var color = "red" | |
var name = "" | |
fun drive() = println("drive") | |
} |
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
import java.math.BigInteger | |
tailrec fun fibonacci(n: Int, a: BigInteger = BigInteger.ZERO, b: BigInteger = BigInteger.ONE): BigInteger { | |
return if (n == 0) { | |
a | |
} else { | |
fibonacci(n - 1, a + b, a) | |
} | |
} | |
fun main() { | |
println(fibonacci(10000)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment