style: auto-format example files with lux fmt
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,55 +1,39 @@
|
||||
// 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))
|
||||
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
|
||||
let result3 = square(addOne(double(5)))
|
||||
|
||||
// Currying example
|
||||
fn add(a: Int): fn(Int): Int =
|
||||
fn(b: Int): Int => a + b
|
||||
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 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))
|
||||
|
||||
Reference in New Issue
Block a user