rewrite execute usiung accumulate, add second test case
This commit is contained in:
parent
574c982368
commit
67bfda490e
14
rover.fnl
14
rover.fnl
@ -43,11 +43,10 @@
|
|||||||
:l {:direction (turn-left rover.direction)}
|
:l {:direction (turn-left rover.direction)}
|
||||||
_ (assert false (. "unrecognised command " string)))))
|
_ (assert false (. "unrecognised command " string)))))
|
||||||
|
|
||||||
(fn execute [rover [cmd & cmds]]
|
(fn execute [rover cmds]
|
||||||
(let [r1 (command rover cmd)]
|
(accumulate [rover rover
|
||||||
(if (next cmds)
|
_ c (ipairs cmds)]
|
||||||
(execute r1 cmds)
|
(merge rover (command rover c))))
|
||||||
r1)))
|
|
||||||
|
|
||||||
(fn rover [x y direction]
|
(fn rover [x y direction]
|
||||||
{: x
|
{: x
|
||||||
@ -99,5 +98,10 @@
|
|||||||
(expect "Rotates to north when pointing west and asked to turn right"
|
(expect "Rotates to north when pointing west and asked to turn right"
|
||||||
(execute r [:r]) {:x 2 :y 4 :direction :n}))
|
(execute r [:r]) {:x 2 :y 4 :direction :n}))
|
||||||
|
|
||||||
|
(let [r (rover 1 1 :s)]
|
||||||
|
(expect "Multiple commands are executed in the right order"
|
||||||
|
(execute r [:l :l :f :f :f :r :f])
|
||||||
|
{:x 2 :y 4 :direction :e}))
|
||||||
|
|
||||||
|
|
||||||
(print "OK")
|
(print "OK")
|
||||||
|
Loading…
Reference in New Issue
Block a user