Files
lux/examples/showcase/higher_order.lux
2026-02-17 06:52:44 -05:00

22 lines
712 B
Plaintext

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))
fn square(n: Int): Int = n * n
fn cube(n: Int): Int = n * n * n
fn makeAdder(n: Int): fn(Int): Int = fn(x: Int): Int => x + n
fn main(): Unit with {Console} = {
Console.print("Square of 5: " + toString(apply(square, 5)))
Console.print("Cube of 3: " + toString(apply(cube, 3)))
let add10 = makeAdder(10)
Console.print("Add 10 to 5: " + toString(add10(5)))
Console.print("Add 10 to 20: " + toString(add10(20)))
let squareThenCube = compose(cube, square)
Console.print("Composed: " + toString(squareThenCube(5)))
}
let output = run main() with {}