Converting roman numerals to numbers using Groovy/Grails

I wrote up a post last week about my experience converting roman numerals to numbers using ColdFusion and I promised a follow up doing the same thing in Grails.

I did learn an interesting tidbit about Grails, the maximum numbers of tests you can have in a where clause is 999. Not a big deal as this is a somewhat contrived example, but something to note nonetheless. Without further ado here is my code, once again this assumes you have entered a valid roman numeral and I’ve tested the accuracy up to 2000.

class RomanService {
  Integer romanToDecimal(String romanNumber) {
    Integer newNumber = 0, previousNumber = 0
    Map romanToNumberMapping = [M:1000, D:500, C:100, L:50, X:10, V:5, I:1]
    for (Integer oneChar = romanNumber.length() - 1; oneChar >= 0; oneChar--) {
      String oneLetter = romanNumber.charAt(oneChar)
      newNumber = processNumber(romanToNumberMapping[oneLetter], previousNumber, newNumber)
      previousNumber = romanToNumberMapping[oneLetter]
    }
    return newNumber 
  }

  Integer processNumber(Integer currentNumber, Integer previousNumber, Integer newNumber) {
    return previousNumber > currentNumber ? newNumber - currentNumber : newNumber + currentNumber
  }
}

On the whole it’s really not much different than the ColdFusion version, semicolons are optional in most places (pretty much anything that’s not a for loop) and you can strongly type the return values, although that’s definitely not a requirement. I wrote the example above exactly how I’d write it for a project, but also wanted to point out some of what I’m doing isn’t really required.

One interesting thing about Groovy and not one I’m terribly fond of is if you don’t have a return statement the last piece of code executed is returned, so newNumber doesn’t need a return statement in the romanToDecimal function and in processNumber the only line is returned. I’ve left the return statements off in the code below, but it’s not something I’d normally do, it saves .1 seconds of typing to not type it and can make the code confusing in my opinion. I also left off the types of variables, but my opinion is the same as not typing out return, it doesn’t save much time to not declare the type and it can cause some unnecesary confusion especially when it’s left off in the arguments.

class RomanService {
  def romanToDecimal(romanNumber) {
    def newNumber = 0, previousNumber = 0
    def romanToNumberMapping = [M:1000, D:500, C:100, L:50, X:10, V:5, I:1]
    for (def oneChar = romanNumber.length() - 1; oneChar >= 0; oneChar--) {
      def oneLetter = romanNumber.charAt(oneChar)
      newNumber = processNumber(romanToNumberMapping[oneLetter], previousNumber, newNumber)
      previousNumber = romanToNumberMapping[oneLetter]
    }
    newNumber
  }

  def processNumber(currentNumber, previousNumber, newNumber) {
    previousNumber > currentNumber ? newNumber - currentNumber : newNumber + currentNumber
  }
}

I’ve attached my test case as an external file given the size

Converting roman numerals to numbers using ColdFusion

I recently worked on a project that required translating roman numerals to the numerical counterpart, this needed to work for anything from 1-2000. I wrote my own using TDD and eventually came up with this

component {
  public function romanToDecimal(romanNumber) {
    var newNumber = 0;
    var previousNumber = 0;
    var romanToNumberMapping = {M:1000, D:500, C:100, L:50, X:10, V:5, I:1};
    var romanNumeral = ucase(romanNumber);
    for (var oneChar = romanNumeral.length() - 1; oneChar >= 0; oneChar--) {
      var oneLetter = romanNumeral.charAt(oneChar);
      if (previousNumber > romanToNumberMapping[oneLetter]) {
        newNumber-=romanToNumberMapping[oneLetter];
      } else {
        newNumber+=romanToNumberMapping[oneLetter];
      }
      previousNumber = romanToNumberMapping[oneLetter];
    }
    return newNumber;
  }
}

It’s simple enough that you should be able to add each roman numeral and it’s number into the mapping and this should theoretically work for any roman numeral, but I only tested up to 2000. This assumes that you have entered a valid roman numeral that can be translated to 1-2000.

The tests are written in MXUnit, so it’s a lot of repetition. I’ll be following up with a groovy example using spock that should have a much cleaner looking test. I’ve attached the test file and additionally have submitted this to cflib for approval.

You can download the test here if you’re so inclined.

Enabling multiple cursors based on a selection in Sublime Text

I was writing a bunch of unit tests for a roman numeral translator function that I’m working on and I was copy/pasting the number and roman numeral from a few websites to validate my function was correction. My plan is to write a test for the first 5000 roman numerals, but using ctrl+d to make 5000 selections and format the selection correctly was going to take quite a while and I knew there had to be a better way.

I googled around for a while and eventually found this Stack Overflow answer. You can enable multiple cursors by making a selection and then pressing ctrl+shift+l (l as in Linux). The cursors will be automatically placed at the end of the line, but you can use ctrl+shift+left arrow key (windows command) to move word by word through your selection.

This option is also available from the Sublime menu by going to Selection > Split into Lines, where it will also tell you the shortcut if you’re not using Windows.

Keep in mind, once you’re past a couple hundred selections Sublime can be very slow even on a fast computer, so be patient if you’re enabling hundreds of cursors.

Using grails assets when you can’t use the asset tag

In Grails 2.4+ the asset pipeline plugin is included by default and you can access files in the assets folder by using an asset tag


  <asset:javascript src="application.js"/>
  <asset:stylesheet src="application.css"/>
  <asset:image src="logo.png" width="200" height="200"/>

However, it’s not always feasible to be able to use that syntax. One example is when loading an svg image with a png fallback. The code would ideally look like this when the HTML is generated.


<svg width="180" height="60">
  <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="logo.svg" src="logo.png"></image>
</svg>

Rather than using an asset tag you can use assetPath() to find the file just like using the asset tag would do. In this case our SVG and PNG images are stored in variables named logoSVG and logoPNG. When the HTML is generated the resulting HTML is as desired.


<svg width="180" height="60">
  <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${assetPath(src: logoSVG)}" src="${assetPath(src: logoPNG)}"></image>
</svg>

One important thing to note is the asset plugin is not recursive, so if your file is not located in assets/images you will need to include the path relative to the images folder in your src. If your images were in an imgs folder the syntax would be like so


<svg width="180" height="60">
  <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${assetPath(src: 'imgs/' + logoSVG)}" src="${assetPath(src: 'imgs/' + logoPNG)}"></image>
</svg>