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>
40 lines
926 B
Plaintext
40 lines
926 B
Plaintext
// 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
|
|
}
|
|
|
|
fn computation(): Int with {Log, Random} = {
|
|
Log.log("Starting computation")
|
|
let x = Random.int(1, 10)
|
|
Log.log("Processing value")
|
|
let result = x * 2
|
|
Log.log("Done")
|
|
result
|
|
}
|
|
|
|
handler consoleLog: Log {
|
|
fn log(msg) = Console.print("[LOG] " + msg)
|
|
}
|
|
|
|
fn main(): Unit with {Console} = {
|
|
let result = run computation() with {
|
|
Log = consoleLog
|
|
}
|
|
Console.print("Generated: " + toString(result / 2))
|
|
Console.print("Result: " + toString(result))
|
|
}
|
|
|
|
let output = run main() with {}
|