make execute a regular function not a method

main
Daniel Barlow 2023-01-22 18:12:58 +00:00
parent db552b8c11
commit 574c982368
1 changed files with 10 additions and 11 deletions

View File

@ -46,14 +46,13 @@
(fn execute [rover [cmd & cmds]]
(let [r1 (command rover cmd)]
(if (next cmds)
(r1:execute cmds)
(execute r1 cmds)
r1)))
(fn rover [x y direction]
{: x
: y
: direction
: execute
})
(let [r (rover 7 15 :n)]
@ -64,41 +63,41 @@
(let [r (rover 0 0 :n)]
(expect "The rover receives a character array of commands"
(r:execute [:f :f :f])
(execute r [:f :f :f])
{}))
(let [r (rover 0 0 :n)]
(expect "Moves north when pointing north and asked to move forward"
(r:execute [:f]) {:x 0 :y 1 :direction :n}))
(execute r [:f]) {:x 0 :y 1 :direction :n}))
(let [r (rover 3 2 :s)]
(expect "Moves south when pointing south and asked to move forward"
(r:execute [:f]) {:x 3 :y 1 :direction :s}))
(execute r [:f]) {:x 3 :y 1 :direction :s}))
(let [r (rover 0 0 :w)]
(expect "Moves west when pointing west and asked to move forward"
(r:execute [:f]) {:x -1 :y 0 :direction :w}))
(execute r [:f]) {:x -1 :y 0 :direction :w}))
(let [r (rover 0 0 :e)]
(expect "Moves east when pointing east and asked to move forward"
(r:execute [:f]) {:x 1 :y 0 :direction :e}))
(execute r [:f]) {:x 1 :y 0 :direction :e}))
(let [r (rover 1 1 :s)]
(expect "The rover acts on multiple commands"
(r:execute [:f :f :f])
(execute r [:f :f :f])
{:x 1 :y -2 :direction :s}))
(let [r (rover 2 4 :e)]
(expect "Rotates to south when pointing east and asked to turn right"
(r:execute [:r]) {:x 2 :y 4 :direction :s}))
(execute r [:r]) {:x 2 :y 4 :direction :s}))
(let [r (rover 2 4 :s)]
(expect "Rotates to east when pointing south and asked to turn left"
(r:execute [:l]) {:x 2 :y 4 :direction :e}))
(execute r [:l]) {:x 2 :y 4 :direction :e}))
(let [r (rover 2 4 :w)]
(expect "Rotates to north when pointing west and asked to turn right"
(r:execute [:r]) {:x 2 :y 4 :direction :n}))
(execute r [:r]) {:x 2 :y 4 :direction :n}))
(print "OK")