make turn-left/right return mergable tables

this gives greater symmetry with `drive`
main
Daniel Barlow 2023-01-22 18:50:38 +00:00
parent 13e283a2a5
commit 7b68206577
1 changed files with 18 additions and 16 deletions

View File

@ -21,30 +21,32 @@
:e {:x (+ x distance)})) :e {:x (+ x distance)}))
(fn turn-left [direction] (fn turn-left [direction]
(-> { {:direction
:n :w (-> {
:w :s :n :w
:s :e :w :s
:e :n :s :e
} :e :n
(. direction))) }
(. direction))})
(fn turn-right [direction] (fn turn-right [direction]
(-> { {:direction
:n :e (-> {
:e :s :n :e
:s :w :e :s
:w :n :s :w
} :w :n
(. direction))) }
(. direction))})
(fn command [rover string] (fn command [rover string]
(merge rover (merge rover
(match string (match string
:f (drive rover 1) :f (drive rover 1)
:b (drive rover -1) :b (drive rover -1)
:r {:direction (turn-right rover.direction)} :r (turn-right rover.direction)
:l {:direction (turn-left rover.direction)} :l (turn-left rover.direction)
_ (assert false (. "unrecognised command " string))))) _ (assert false (. "unrecognised command " string)))))
(fn execute [rover cmds] (fn execute [rover cmds]