Task

Create a function that takes a Roman numeral as its argument and returns its value as a numeric decimal integer.

You don't need to validate the form of the Roman numeral.

Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately,

starting with the leftmost decimal digit and skipping any 0s   (zeroes).

1990 is rendered as   MCMXC     (1000 = M,   900 = CM,   90 = XC)     and

2008 is rendered as   MMVIII       (2000 = MM,   8 = VIII).

The Roman numeral for 1666,   MDCLXVI,   uses each letter in descending order.




#!/usr/bin/env golosh
----
This module converts a Roman numeral into a decimal number.
----
module Romannumeralsdecode
 
augment java.lang.Character {
 
  function decode = |this| -> match {
    when this == 'I' then 1
    when this == 'V' then 5
    when this == 'X' then 10
    when this == 'L' then 50
    when this == 'C' then 100
    when this == 'D' then 500
    when this == 'M' then 1000
    otherwise 0
  }
}
 
augment java.lang.String {
 
  function decode = |this| {
    var accumulator = 0
    foreach i in [0..this: length()] {
      let currentChar = this: charAt(i)
      let nextChar = match {
        when i + 1 < this: length() then this: charAt(i + 1)
        otherwise null
      }
      if (currentChar: decode() < (nextChar?: decode() orIfNull 0)) {
        # if this is something like IV or IX or whatever
        accumulator = accumulator - currentChar: decode()
      } else {
        accumulator = accumulator + currentChar: decode()
      }
    }
    return accumulator
  }
}
 
function main = |args| {
  println("MCMXC = " + "MCMXC": decode())
  println("MMVIII = " + "MMVIII": decode())
  println("MDCLXVI = " + "MDCLXVI": decode())
}
 