Sunday, March 8, 2015

Mars Rover code version using protocols instead of multimethods

I've continued working on the Mars Rovers kata code that I first implemented using multimethods.

In this version I've used protocols instead of multimethods.

Since I had previously distributed the code in several different name spaces, I only had to modify the rover name space.

The rest of the code keeps just using the same functions of the rover name space: the rover factory and the four commands rotate-left, rotate-right, move-forwards and move-backwards.

However, the implementation of the rover name space is quite different now.

This is the new code:

I've defined two protocols Rotable and Movable which are implemented by new four defined records: FacingNorth, FacingSouth, FacingWest and FacingEast.

Depending on the value of the rover direction, the oriented-rover factory function, creates an instance of one of those records.

When one of the command functions (rotate-left, rotate-right, move-forwards or move-backwards) is called, it calls a function with the same name passing it the result of calling the oriented-rover factory on the rover.
This call is dispatched on the type of the record the call to oriented-rover returns, so that, in the end, the right implementation of the function gets called.

This has been a nice exercise to explore and practice with protocols.

I'll continue exploring other ways to solve this kata with Clojure.

You can find the code in this GitHub repository.

-----------

Update: I continued working in this code on Mars Rover using a finite state machine implemented with mutually recursive functions and trampoline

No comments:

Post a Comment