Skip to content
Solving a simple problem in Kotlin using Functional Programming
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Mars Rover Functional Programming

I have solved the problem defined HERE using functional programming.

You can see how concise this code looks compared to the OOP approach used to solve the same problem here

I have used fold to reduce the the sequence of instructions to a single final position, which is the destination expected.

Fold takes an accumulator and element of list/map, combines/reduces and returns this value as accumulator to next element and so on, till the last element is completed.

Fold is similar to Reduce except that it takes and initial value, whereas Reduce copies the first element to the accumulator. Also Fold can be used when the accumulator is of a different type than the list or map.

The crux of the problem is converting the sequence of instructions given as a string to a position on the grid.

So, given instructions 'MML' tells the rover to move two spaces in which ever direction it is facing and the turn left.

  • Split the string(ins) into a Character Sequence.
  • Pass the initial position of Rover to Fold
  • For each char instruction('L', 'R' or 'M'), turn left, right or move the rover respectively.

Below is the code that performs the above steps

ins.fold(position) { acc: RoverPosition, ins ->
            when (ins) {
                'L' -> acc.left()
                'R' -> acc.right()
                else -> acc.move(pl)
You can’t perform that action at this time.