style: auto-format example files with lux fmt

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-17 06:52:44 -05:00
parent 8c90d5a8dc
commit 44ea1eebb0
54 changed files with 580 additions and 1483 deletions

View File

@@ -1,15 +1,3 @@
// The "Ask" Pattern - Resumable Effects
//
// Unlike exceptions which unwind the stack, effect handlers can
// RESUME with a value. This enables "ask the environment" patterns.
//
// Expected output:
// Need config: api_url
// Got: https://api.example.com
// Need config: timeout
// Got: 30
// Configured with url=https://api.example.com, timeout=30
effect Config {
fn get(key: String): String
}
@@ -25,14 +13,13 @@ fn configure(): String with {Config, Console} = {
}
handler envConfig: Config {
fn get(key) =
if key == "api_url" then resume("https://api.example.com")
else if key == "timeout" then resume("30")
else resume("unknown")
fn get(key) = if key == "api_url" then resume("https://api.example.com") else if key == "timeout" then resume("30") else resume("unknown")
}
fn main(): Unit with {Console} = {
let result = run configure() with { Config = envConfig }
let result = run configure() with {
Config = envConfig,
}
Console.print(result)
}

View File

@@ -1,15 +1,3 @@
// Custom Logging with Effects
//
// This demonstrates how effects let you abstract side effects.
// The same code can be run with different logging implementations.
//
// Expected output:
// [INFO] Starting computation
// [DEBUG] x = 10
// [INFO] Processing
// [DEBUG] result = 20
// Final: 20
effect Log {
fn info(msg: String): Unit
fn debug(msg: String): Unit
@@ -26,18 +14,22 @@ fn computation(): Int with {Log} = {
}
handler consoleLogger: Log {
fn info(msg) = {
Console.print("[INFO] " + msg)
resume(())
}
fn debug(msg) = {
Console.print("[DEBUG] " + msg)
resume(())
}
fn info(msg) =
{
Console.print("[INFO] " + msg)
resume(())
}
fn debug(msg) =
{
Console.print("[DEBUG] " + msg)
resume(())
}
}
fn main(): Unit with {Console} = {
let result = run computation() with { Log = consoleLogger }
let result = run computation() with {
Log = consoleLogger,
}
Console.print("Final: " + toString(result))
}

View File

@@ -1,37 +1,18 @@
// Early Return with Fail Effect
//
// The Fail effect provides clean early termination.
// Functions declare their failure modes in the type signature.
//
// Expected output:
// Parsing "42"...
// Result: 42
// Parsing "100"...
// Result: 100
// Dividing 100 by 4...
// Result: 25
fn parsePositive(s: String): Int with {Fail, Console} = {
Console.print("Parsing \"" + s + "\"...")
if s == "42" then 42
else if s == "100" then 100
else Fail.fail("Invalid number: " + s)
if s == "42" then 42 else if s == "100" then 100 else Fail.fail("Invalid number: " + s)
}
fn safeDivide(a: Int, b: Int): Int with {Fail, Console} = {
Console.print("Dividing " + toString(a) + " by " + toString(b) + "...")
if b == 0 then Fail.fail("Division by zero")
else a / b
if b == 0 then Fail.fail("Division by zero") else a / b
}
fn main(): Unit with {Console} = {
// These succeed
let n1 = run parsePositive("42") with {}
Console.print("Result: " + toString(n1))
let n2 = run parsePositive("100") with {}
Console.print("Result: " + toString(n2))
let n3 = run safeDivide(100, 4) with {}
Console.print("Result: " + toString(n3))
}

View File

@@ -1,16 +1,3 @@
// Effect Composition - Combine multiple effects cleanly
//
// Unlike monad transformers (which have ordering issues),
// effects can be freely combined without boilerplate.
// Each handler handles its own effect, ignoring others.
//
// Expected output:
// [LOG] Starting computation
// Generated: 7
// [LOG] Processing value
// [LOG] Done
// Result: 14
effect Log {
fn log(msg: String): Unit
}
@@ -30,8 +17,8 @@ handler consoleLog: Log {
fn main(): Unit with {Console} = {
let result = run computation() with {
Log = consoleLog
}
Log = consoleLog,
}
Console.print("Generated: " + toString(result / 2))
Console.print("Result: " + toString(result))
}

View File

@@ -1,38 +1,19 @@
// Higher-Order Functions and Closures
//
// Functions are first-class values in Lux.
// Closures capture their environment.
//
// Expected output:
// Square of 5: 25
// Cube of 3: 27
// Add 10 to 5: 15
// Add 10 to 20: 30
// Composed: 15625 (cube(square(5)) = cube(25) = 15625)
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))
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 makeAdder(n: Int): fn(Int): Int = fn(x: Int): Int => x + n
fn main(): Unit with {Console} = {
// Apply functions
Console.print("Square of 5: " + toString(apply(square, 5)))
Console.print("Cube of 3: " + toString(apply(cube, 3)))
// Closures
let add10 = makeAdder(10)
Console.print("Add 10 to 5: " + toString(add10(5)))
Console.print("Add 10 to 20: " + toString(add10(20)))
// Function composition
let squareThenCube = compose(cube, square)
Console.print("Composed: " + toString(squareThenCube(5)))
}

View File

@@ -1,16 +1,3 @@
// Algebraic Data Types and Pattern Matching
//
// Lux has powerful ADTs with exhaustive pattern matching.
// The type system ensures all cases are handled.
//
// Expected output:
// Evaluating: (2 + 3)
// Result: 5
// Evaluating: ((1 + 2) * (3 + 4))
// Result: 21
// Evaluating: (10 - (2 * 3))
// Result: 4
type Expr =
| Num(Int)
| Add(Expr, Expr)
@@ -19,19 +6,19 @@ type Expr =
fn eval(e: Expr): Int =
match e {
Num(n) => n,
Add(a, b) => eval(a) + eval(b),
Sub(a, b) => eval(a) - eval(b),
Mul(a, b) => eval(a) * eval(b)
}
Num(n) => n,
Add(a, b) => eval(a) + eval(b),
Sub(a, b) => eval(a) - eval(b),
Mul(a, b) => eval(a) * eval(b),
}
fn showExpr(e: Expr): String =
match e {
Num(n) => toString(n),
Add(a, b) => "(" + showExpr(a) + " + " + showExpr(b) + ")",
Sub(a, b) => "(" + showExpr(a) + " - " + showExpr(b) + ")",
Mul(a, b) => "(" + showExpr(a) + " * " + showExpr(b) + ")"
}
Num(n) => toString(n),
Add(a, b) => "(" + showExpr(a) + " + " + showExpr(b) + ")",
Sub(a, b) => "(" + showExpr(a) + " - " + showExpr(b) + ")",
Mul(a, b) => "(" + showExpr(a) + " * " + showExpr(b) + ")",
}
fn evalAndPrint(e: Expr): Unit with {Console} = {
Console.print("Evaluating: " + showExpr(e))
@@ -39,15 +26,10 @@ fn evalAndPrint(e: Expr): Unit with {Console} = {
}
fn main(): Unit with {Console} = {
// (2 + 3)
let e1 = Add(Num(2), Num(3))
evalAndPrint(e1)
// ((1 + 2) * (3 + 4))
let e2 = Mul(Add(Num(1), Num(2)), Add(Num(3), Num(4)))
evalAndPrint(e2)
// (10 - (2 * 3))
let e3 = Sub(Num(10), Mul(Num(2), Num(3)))
evalAndPrint(e3)
}