December 24, 2013
On Monday, 23 December 2013 at 22:46:24 UTC, Gary Willoughby wrote:
> I like it and it seems you are grasping D well. I wonder if the entire implementation could be done using templates and all done at compile time? It would be interesting to explore.
>
> int year    = fromRoman!("DXLIX");
> string year = toRoman!(2013);
>
> Very similar to how [octal][1] is implemented;
>
> [1]: http://dlang.org/phobos/std_conv.html#.octal

I was about to submit an "eval" function which would render this kind of stuff obsolete. The idea is that since D has CTFE built-in, such templates are not needed, since you can do:
enum year = fromRoman("DXLIX");

The "problem" is that is you want a *variable* that is initialized *to* a certain value, but don't want to pay for the initialization, you have to write:
enum yearEnum = fromRoman("DXLIX");
int year = yearEnum;

"eval" (which is documented in the docs, but isn't in phobos), would allow:

int year = eval!(fromRoman!("DXLIX"));

I think such a generic solution is a better approach, and generally useful regardless of what you are dealing with.
December 25, 2013
> 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.

-- 
Dejan Lekic
dejan.lekic (a) gmail.com
http://dejan.lekic.org
December 25, 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
1 2
Next ›   Last »