feat: add comprehensive example programs
Standard examples (examples/standard/): - hello_world: Basic effect usage - fizzbuzz: Classic programming exercise - factorial: Recursive and tail-recursive versions - primes: Prime number generation - guessing_game: Interactive Random + Console effects - stdlib_demo: Demonstrates List, String, Option, Math modules Showcase examples (examples/showcase/): - ask_pattern: Resumable effects for config/environment - custom_logging: Custom effect with handler - early_return: Fail effect for clean error handling - effect_composition: Combining multiple effects - higher_order: Closures and function composition - pattern_matching: ADTs and exhaustive matching Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
43
examples/standard/stdlib_demo.lux
Normal file
43
examples/standard/stdlib_demo.lux
Normal file
@@ -0,0 +1,43 @@
|
||||
// Standard Library Demo
|
||||
// Demonstrates the built-in modules: List, String, Option, Math
|
||||
|
||||
fn main(): Unit with {Console} = {
|
||||
Console.print("=== List Operations ===")
|
||||
let nums = [1, 2, 3, 4, 5]
|
||||
Console.print("Original: " + toString(nums))
|
||||
Console.print("Mapped (*2): " + toString(List.map(nums, fn(x: Int): Int => x * 2)))
|
||||
Console.print("Filtered (even): " + toString(List.filter(nums, fn(x: Int): Bool => x % 2 == 0)))
|
||||
Console.print("Sum (fold): " + toString(List.fold(nums, 0, fn(acc: Int, x: Int): Int => acc + x)))
|
||||
Console.print("Length: " + toString(List.length(nums)))
|
||||
Console.print("Reversed: " + toString(List.reverse(nums)))
|
||||
Console.print("Range 1-5: " + toString(List.range(1, 6)))
|
||||
|
||||
Console.print("")
|
||||
Console.print("=== String Operations ===")
|
||||
let text = " Hello, World! "
|
||||
Console.print("Original: \"" + text + "\"")
|
||||
Console.print("Trimmed: \"" + String.trim(text) + "\"")
|
||||
Console.print("Upper: " + String.toUpper(text))
|
||||
Console.print("Lower: " + String.toLower(text))
|
||||
Console.print("Contains 'World': " + toString(String.contains(text, "World")))
|
||||
Console.print("Split by comma: " + toString(String.split("a,b,c", ",")))
|
||||
Console.print("Join with dash: " + String.join(["x", "y", "z"], "-"))
|
||||
|
||||
Console.print("")
|
||||
Console.print("=== Option Operations ===")
|
||||
let some_val = Some(42)
|
||||
let none_val: Option<Int> = None
|
||||
Console.print("Some(42) mapped (*2): " + toString(Option.map(some_val, fn(x: Int): Int => x * 2)))
|
||||
Console.print("None mapped: " + toString(Option.map(none_val, fn(x: Int): Int => x * 2)))
|
||||
Console.print("Some(42) getOrElse(0): " + toString(Option.getOrElse(some_val, 0)))
|
||||
Console.print("None getOrElse(0): " + toString(Option.getOrElse(none_val, 0)))
|
||||
|
||||
Console.print("")
|
||||
Console.print("=== Math Operations ===")
|
||||
Console.print("abs(-5): " + toString(Math.abs(-5)))
|
||||
Console.print("min(3, 7): " + toString(Math.min(3, 7)))
|
||||
Console.print("max(3, 7): " + toString(Math.max(3, 7)))
|
||||
Console.print("pow(2, 10): " + toString(Math.pow(2, 10)))
|
||||
}
|
||||
|
||||
let output = run main() with {}
|
||||
Reference in New Issue
Block a user