extract `drive` function which can go forward and backward

main
Daniel Barlow 2023-01-22 18:47:54 +00:00
parent a4ac0dfe52
commit 13e283a2a5
1 changed files with 9 additions and 10 deletions

View File

@ -13,6 +13,13 @@
:actual actual# :actual actual#
}))))) })))))
(fn drive [{: x : y : direction} distance]
(match direction
:n {:y (+ y distance)}
:s {:y (- y distance)}
:w {:x (- x distance)}
:e {:x (+ x distance)}))
(fn turn-left [direction] (fn turn-left [direction]
(-> { (-> {
:n :w :n :w
@ -34,16 +41,8 @@
(fn command [rover string] (fn command [rover string]
(merge rover (merge rover
(match string (match string
:f (match rover.direction :f (drive rover 1)
:n {:y (+ rover.y 1)} :b (drive rover -1)
:s {:y (- rover.y 1)}
:w {:x (- rover.x 1)}
:e {:x (+ rover.x 1)})
:b (match rover.direction
:n {:y (- rover.y 1)}
:s {:y (+ rover.y 1)}
:w {:x (+ rover.x 1)}
:e {:x (- rover.x 1)})
:r {:direction (turn-right rover.direction)} :r {:direction (turn-right rover.direction)}
:l {:direction (turn-left rover.direction)} :l {:direction (turn-left rover.direction)}
_ (assert false (. "unrecognised command " string))))) _ (assert false (. "unrecognised command " string)))))