// 1. Functions can be considered a form of data! // How to describe their type? Signature! That is... // What data type(s) they take in, what data they produce fun addNums(a: Int, b: Int): Int { return a + b } fun multNums(a: Int, b: Int): Int { return a * b } fun outputResult(a: Int, b: Int, operation: (Int, Int)->Int) { val result: Int = operation(a, b) println("Result: $result") } // outputResult(3, 5, ::addNums) // outputResult(3, 5, ::multNums) // 2. Functions can *produce* other functions :) // Why? Sometimes you need information to "customize" // the pattern of instructions in a function. // // Note: generally we avoid (can be confusing), but // we often *need* to (e.g., a function is // constrained in what arguments it can accept). // // represents hours till boarding val flightBoards = 8 val aliceBuffer = 1 val bobBuffer = 3 // produces a personalized calculator based on your buffer time fun makeBufferApp(myBuffer: Int): (Int) -> Int { fun myBufferApp(whenFlightBoards: Int): Int { return whenFlightBoards - myBuffer } return ::myBufferApp } val bobBufferTime = makeBufferApp(bobBuffer) println("bob: ${ bobBufferTime(flightBoards) }") // println("bob: ${ bobBufferTime(12) }") // println("bob: ${ bobBufferTime(4) }") // and if we are only going to use it once, could... println("alice: ${ makeBufferApp(aliceBuffer)(flightBoards) }")