// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: turtle.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // A turtle you drive with a CHAIN of commands — the fluent-builder pattern, and the // reason it works. // // The turtle is a `ref data`: it has identity and evolving state (position + heading). // Each command is applied by `apply`, which mutates the turtle and then **returns the // turtle itself**. Because a `ref data` is a reference, returning it hands back the SAME // object — so `apply` can be chained, and you never re-name the receiver: // // Turtle().apply(...).apply(...).apply(...) // // That return-`self` move is the whole trick behind fluent APIs (`StringBuilder`, // LINQ, query builders). Contrast it with a method that returns a `Result` or a fresh // value: those don't chain, because the next call would land on the wrong type. // // The commands themselves show the other half of the language: a `choice` whose // `forward` case carries a step count and whose `turn` case carries an `enum` // direction, dispatched with `match`. One `apply`, two payload shapes, no `if`-ladder. // ═════════════════════════════════════════════════════════════════════════════ // Which way to pivot. A plain `enum` — a closed set of named constants. enum Turn { left right } // What the turtle can be told to do. A `choice`: `forward` carries how far, `turn` // carries which way (the `Turn` enum). `match` in `apply` handles both. choice Command { forward(steps: int) turn(direction: Turn) } // The turtle. `ref data` = identity + mutable state; heading is 0=N, 1=E, 2=S, 3=W, // so a right turn is +1 (mod 4) and a left turn is +3 (mod 4). ref data Turtle { var x: int var y: int var facing: int init() { self.x = 0 self.y = 0 self.facing = 0 // start facing North } } // Apply one command and RETURN THE TURTLE — that return-self is what makes `apply` // chainable. `match` dispatches the command; `forward` walks in the current heading, // `turn` rotates. The nested `match (direction: Turn)` folds the enum to a delta. func apply(t: Turtle, cmd: Command) -> Turtle { match cmd { .forward(steps) { if t.facing == 0 { t.y += steps } // North else if t.facing == 1 { t.x += steps } // East else if t.facing == 2 { t.y -= steps } // South else { t.x -= steps } // West } .turn(direction) { match (direction: Turn) { .left { t.facing = (t.facing + 3) % 4 } .right { t.facing = (t.facing + 1) % 4 } } } } return t // the SAME turtle — the chain continues on it } // Drive the turtle with a single fluent chain — no intermediate `t` rebinding, just // commands flowing through the one object: // // start (0,0) facing N // forward 5 → (0,5) turn right → facing E // forward 3 → (3,5) turn right → facing S // forward 2 → (3,3) // // Final position (3, 3) → encoded as x*100 + y = 303. func main() -> int { let t = Turtle() .apply(Command.forward(5)) .apply(Command.turn(Turn.right())) .apply(Command.forward(3)) .apply(Command.turn(Turn.right())) .apply(Command.forward(2)) return t.x * 100 + t.y // 303 }