feat: implement Random and Time built-in effects
Add Random and Time effects for random number generation and time-based operations. These effects can be used in any effectful code block. Random effect operations: - Random.int(min, max) - random integer in range [min, max] - Random.float() - random float in range [0.0, 1.0) - Random.bool() - random boolean Time effect operations: - Time.now() - current Unix timestamp in milliseconds - Time.sleep(ms) - sleep for specified milliseconds Changes: - Add rand crate dependency - Add Random and Time effect definitions to types.rs - Add effects to built-in effects list in typechecker - Implement effect handlers in interpreter - Add 4 new tests for Random and Time effects - Add examples/random.lux demonstrating usage Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
41
examples/random.lux
Normal file
41
examples/random.lux
Normal file
@@ -0,0 +1,41 @@
|
||||
// Demonstrating Random and Time effects in Lux
|
||||
//
|
||||
// Expected output (values will vary):
|
||||
// Rolling dice...
|
||||
// Die 1: <random 1-6>
|
||||
// Die 2: <random 1-6>
|
||||
// Die 3: <random 1-6>
|
||||
// Coin flip: <true/false>
|
||||
// Random float: <0.0-1.0>
|
||||
// Current time: <timestamp>
|
||||
|
||||
// Roll a single die (1-6)
|
||||
fn rollDie(): Int with {Random} = Random.int(1, 6)
|
||||
|
||||
// Roll multiple dice and print results
|
||||
fn rollDice(count: Int): Unit with {Random, Console} = {
|
||||
if count > 0 then {
|
||||
let value = rollDie()
|
||||
Console.print("Die " + toString(4 - count) + ": " + toString(value))
|
||||
rollDice(count - 1)
|
||||
} else {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
// Main function demonstrating random effects
|
||||
fn main(): Unit with {Random, Console, Time} = {
|
||||
Console.print("Rolling dice...")
|
||||
rollDice(3)
|
||||
|
||||
let coin = Random.bool()
|
||||
Console.print("Coin flip: " + toString(coin))
|
||||
|
||||
let f = Random.float()
|
||||
Console.print("Random float: " + toString(f))
|
||||
|
||||
let now = Time.now()
|
||||
Console.print("Current time: " + toString(now))
|
||||
}
|
||||
|
||||
let output = run main() with {}
|
||||
Reference in New Issue
Block a user