Files
lux/examples/showcase/ask_pattern.lux
Brandon Lucas 0b5abece5f 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>
2026-02-13 17:25:04 -05:00

40 lines
1.1 KiB
Plaintext

// 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
}
fn configure(): String with {Config, Console} = {
Console.print("Need config: api_url")
let url = Config.get("api_url")
Console.print("Got: " + url)
Console.print("Need config: timeout")
let timeout = Config.get("timeout")
Console.print("Got: " + timeout)
"Configured with url=" + url + ", timeout=" + timeout
}
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 main(): Unit with {Console} = {
let result = run configure() with { Config = envConfig }
Console.print(result)
}
let output = run main() with {}