In the last guide I implemented an XML parser that counted all of the lines from Shakespeare’s Macbeth play. That implementation worked, however it used an iterative approach, which resulted in a highly coupled method that would only be able to work on another XML file if it had an identical structure and set of tag names. In this guide we’re going to refactor the solution to utilize a functional approach.
Refactor the Macbeth XML parser/counter so it uses a functional programming approach.
Credit to Upcase for the great coding exercise.
macbeth_parser method into multiple methods that utilize functional programming calls instead of the iterative approach. The RSpec tests should still pass, which means that the resulting output should be identical to the current implementation.
Real World Usage
This solution was more challenging to build out than the iterative approach. However the result will be a more scalable implementation that can be utilized by other XML files. Anytime that you are building a program, it’s typically a good idea (and a good practice) to design with modularity in mind.
Can be found on the solutions branch on github.