commit 13e283a2a592eba668773c4d58254076be7b41f0 parent a4ac0dfe52e1859a8e722108c1e661941a3a1f10 Author: Daniel Barlow <dan@telent.net> Date: Sun, 22 Jan 2023 18:47:54 +0000 extract `drive` function which can go forward and backward Diffstat:
| M | rover.fnl | | | 19 | +++++++++---------- |
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/rover.fnl b/rover.fnl @@ -13,6 +13,13 @@ :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] (-> { :n :w @@ -34,16 +41,8 @@ (fn command [rover string] (merge rover (match string - :f (match rover.direction - :n {:y (+ rover.y 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)}) + :f (drive rover 1) + :b (drive rover -1) :r {:direction (turn-right rover.direction)} :l {:direction (turn-left rover.direction)} _ (assert false (. "unrecognised command " string)))))