Created
May 20, 2018 17:16
-
-
Save genju83/5dede09d37098b332a5aa6c280a1cc19 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(args: Array<String>) { | |
println(sum(1, 3)) // 4 | |
println(sum2(1, 3)) // 4 | |
println(multiply(1, 3)) // 3 | |
println(subtract(1, 3)) // -2 | |
} | |
fun sum(a: Int, b: Int) | |
= higherOrderFunction(a, b, { a, b -> a + b }) | |
fun sum2(a: Int, b: Int) | |
= higherOrderFunction(a, b) { a, b -> a + b } | |
fun multiply(a: Int, b: Int) | |
= higherOrderFunction(a, b, { a, b -> a * b }) | |
fun subtract(a: Int, b: Int) | |
= higherOrderFunction(a, b, { a, b -> a - b }) | |
fun higherOrderFunction(a: Int, b: Int, block: (Int, Int) -> Int): Int { | |
return block(a, b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment