// Demonstrating functional programming features // // Expected output: // apply(double, 21) = 42 // compose(addOne, double)(5) = 11 // pipe: 5 |> double |> addOne |> square = 121 // curried add5(10) = 15 // partial times3(7) = 21 // record transform = 30 // Higher-order functions fn apply(f: fn(Int): Int, x: Int): Int = f(x) fn compose(f: fn(Int): Int, g: fn(Int): Int): fn(Int): Int = fn(x: Int): Int => f(g(x)) // Basic functions fn double(x: Int): Int = x * 2 fn addOne(x: Int): Int = x + 1 fn square(x: Int): Int = x * x // Using apply let result1 = apply(double, 21) // Using compose let doubleAndAddOne = compose(addOne, double) let result2 = doubleAndAddOne(5) // Using the pipe operator let result3 = 5 |> double |> addOne |> square // Currying example fn add(a: Int): fn(Int): Int = fn(b: Int): Int => a + b let add5 = add(5) let result4 = add5(10) // Partial application simulation fn multiply(a: Int, b: Int): Int = a * b let times3 = fn(x: Int): Int => multiply(3, x) let result5 = times3(7) // Working with records let transform = fn(record: { x: Int, y: Int }): Int => record.x + record.y let point = { x: 10, y: 20 } let recordSum = transform(point) // Print all results fn printResults(): Unit with {Console} = { Console.print("apply(double, 21) = " + toString(result1)) Console.print("compose(addOne, double)(5) = " + toString(result2)) Console.print("pipe: 5 |> double |> addOne |> square = " + toString(result3)) Console.print("curried add5(10) = " + toString(result4)) Console.print("partial times3(7) = " + toString(result5)) Console.print("record transform = " + toString(recordSum)) } let output = run printResults() with {}