style: auto-format example files with lux fmt
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,3 @@
|
||||
// State machine example using algebraic data types
|
||||
// Demonstrates pattern matching for state transitions
|
||||
//
|
||||
// Expected output:
|
||||
// Initial light: red
|
||||
// After transition: green
|
||||
// After two transitions: yellow
|
||||
// Door: Closed -> Open -> Closed -> Locked
|
||||
|
||||
// Traffic light state machine
|
||||
type TrafficLight =
|
||||
| Red
|
||||
| Yellow
|
||||
@@ -15,26 +5,25 @@ type TrafficLight =
|
||||
|
||||
fn nextLight(light: TrafficLight): TrafficLight =
|
||||
match light {
|
||||
Red => Green,
|
||||
Green => Yellow,
|
||||
Yellow => Red
|
||||
}
|
||||
Red => Green,
|
||||
Green => Yellow,
|
||||
Yellow => Red,
|
||||
}
|
||||
|
||||
fn canGo(light: TrafficLight): Bool =
|
||||
match light {
|
||||
Green => true,
|
||||
Yellow => false,
|
||||
Red => false
|
||||
}
|
||||
Green => true,
|
||||
Yellow => false,
|
||||
Red => false,
|
||||
}
|
||||
|
||||
fn lightColor(light: TrafficLight): String =
|
||||
match light {
|
||||
Red => "red",
|
||||
Yellow => "yellow",
|
||||
Green => "green"
|
||||
}
|
||||
Red => "red",
|
||||
Yellow => "yellow",
|
||||
Green => "green",
|
||||
}
|
||||
|
||||
// Door state machine
|
||||
type DoorState =
|
||||
| Open
|
||||
| Closed
|
||||
@@ -48,31 +37,34 @@ type DoorAction =
|
||||
|
||||
fn applyAction(state: DoorState, action: DoorAction): DoorState =
|
||||
match (state, action) {
|
||||
(Closed, OpenDoor) => Open,
|
||||
(Open, CloseDoor) => Closed,
|
||||
(Closed, LockDoor) => Locked,
|
||||
(Locked, UnlockDoor) => Closed,
|
||||
_ => state
|
||||
}
|
||||
(Closed, OpenDoor) => Open,
|
||||
(Open, CloseDoor) => Closed,
|
||||
(Closed, LockDoor) => Locked,
|
||||
(Locked, UnlockDoor) => Closed,
|
||||
_ => state,
|
||||
}
|
||||
|
||||
fn doorStateName(state: DoorState): String =
|
||||
match state {
|
||||
Open => "Open",
|
||||
Closed => "Closed",
|
||||
Locked => "Locked"
|
||||
}
|
||||
Open => "Open",
|
||||
Closed => "Closed",
|
||||
Locked => "Locked",
|
||||
}
|
||||
|
||||
// Test the state machines
|
||||
let light1 = Red
|
||||
|
||||
let light2 = nextLight(light1)
|
||||
|
||||
let light3 = nextLight(light2)
|
||||
|
||||
let door1 = Closed
|
||||
|
||||
let door2 = applyAction(door1, OpenDoor)
|
||||
|
||||
let door3 = applyAction(door2, CloseDoor)
|
||||
|
||||
let door4 = applyAction(door3, LockDoor)
|
||||
|
||||
// Print results
|
||||
fn printResults(): Unit with {Console} = {
|
||||
Console.print("Initial light: " + lightColor(light1))
|
||||
Console.print("After transition: " + lightColor(light2))
|
||||
|
||||
Reference in New Issue
Block a user