My first D module - Critiques welcome.

Artur Skawina art.08.09 at gmail.com
Wed Dec 25 07:21:35 PST 2013


On 12/25/13 15:07, Dejan Lekic wrote:
>> You could also do some neat stuff with opDispatch.  Someone
>> actually
>> wrote an article about using it with roman numerals:
>> http://idorobots.org/2012/03/04/romans-rubies-and-the-d/
> 
> The idea is actually brilliant. :)
> I think I may use it in the future when I need to deal with roman numbers.

Note that the "D’s version has no runtime overhead at all" part is not true -
- there still is the overhead of a function call (which he admits to later).

A truly overhead-less version would be:

   struct Roman {
      template opDispatch(string number) {
         enum num = number.replace("IV", "IIII")
                          .replace("IX", "VIIII")
                          .replace("XL", "XXXX")
                          .replace("XC", "LXXXX");

         enum opDispatch = num.count('I')
                    + num.count('V') * 5
                    + num.count('X') * 10
                    + num.count('L') * 50
                    + num.count('C') * 100;
      }
   }

artur


More information about the Digitalmars-d-learn mailing list