Created
May 6, 2022 05:18
-
-
Save AndroidPoet/c174f76c16acd2440a42a9d2eda5af7f to your computer and use it in GitHub Desktop.
Higher-Order Functions
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 sumResult = calculate(4, 5, ::sum) //sumResult 9, | |
val mulResult = calculate(4, 5) { a, b -> a * b } // mulResult 20 | |
println("sumResult $sumResult, mulResult $mulResult") | |
} | |
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int { | |
return operation(x, y) | |
} | |
fun sum(x: Int, y: Int) = x + y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment