Skip to content

Instantly share code, notes, and snippets.

@genju83
Created May 20, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genju83/5dede09d37098b332a5aa6c280a1cc19 to your computer and use it in GitHub Desktop.
Save genju83/5dede09d37098b332a5aa6c280a1cc19 to your computer and use it in GitHub Desktop.
higher-order functions
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