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>
45 lines
996 B
Plaintext
45 lines
996 B
Plaintext
// 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
|
|
}
|
|
|
|
fn computation(): Int with {Log} = {
|
|
Log.info("Starting computation")
|
|
let x = 10
|
|
Log.debug("x = " + toString(x))
|
|
Log.info("Processing")
|
|
let result = x * 2
|
|
Log.debug("result = " + toString(result))
|
|
result
|
|
}
|
|
|
|
handler consoleLogger: Log {
|
|
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 }
|
|
Console.print("Final: " + toString(result))
|
|
}
|
|
|
|
let output = run main() with {}
|