A higher order function returning a function delegate, and a dynamic function
The complete source of functions_higher_order.ek9, compiled by the EK9 compiler. Shown on functions.html.
#!ek9 defines module site.functions.higherorder defines constant HEAVY_THRESHOLD <- 21 defines function mathOperation() as pure abstract -> amount as Float <- result as Float? multiply() is mathOperation as pure -> amount as Float <- result as Float: amount * 5.6 heavyMultiply() is mathOperation as pure -> amount as Float <- result as Float: amount * 7.99 suitableMathOperation() -> amount as Float <- op as mathOperation: amount < HEAVY_THRESHOLD <- heavyMultiply else multiply defines program ShowHighOrderFunction() stdout <- Stdout() amount <- 9 mathOp <- suitableMathOperation(amount) calculatedResult <- mathOp(amount) stdout.println(`The result is ${calculatedResult}`) //Now a dynamic function example. inlineFunction <- ( v1: 12, v2: 64, v3: 90 ) is mathOperation as pure (result: v1 + v2 + v3 * amount) additionalCalculation <- inlineFunction(amount) // 12 + 64 + 90*9 stdout.println(`The additional result is ${additionalCalculation}`) value1 <- 9 value2 <- 30 chainedResult <- suitableMathOperation(value1)(value2) stdout.println(`The chained result is ${chainedResult}`)
Download functions_higher_order.ek9
Output
The result is 71.91 The additional result is 886.0 The chained result is 239.70000000000002