Skip to content

Instantly share code, notes, and snippets.

@AndroidPoet
Created May 6, 2022 05:18
Show Gist options
  • Save AndroidPoet/c174f76c16acd2440a42a9d2eda5af7f to your computer and use it in GitHub Desktop.
Save AndroidPoet/c174f76c16acd2440a42a9d2eda5af7f to your computer and use it in GitHub Desktop.
Higher-Order Functions
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