Rosetta Commatizing numbers

Jordan Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 30 21:10:21 PDT 2017


On Tuesday, 30 May 2017 at 10:54:49 UTC, Solomon E wrote:
>
> The earlier version of the page made D look more error prone 
> than other languages, but short. Now my solution is as long as 
> some of the other language's solutions, but it's well commented 
> and tested, I think. Now I doubt any of the solutions in other 
> languages are as correct or potentially useful or informative.
>

Regardless to solution length...one place to make code a little 
shorter could be when you check for special prefix; maybe replace 
the two foreach loops? Something like:

if (specials != null) {
     // There may be special prefixed formats that use different 
separators.
     // Any format with a longer prefix should override a shorter 
one.
     auto pairs = specials.byKeyValue
                          .array
                          .sort!((a,b) => a.key.length < 
b.key.length);

     auto preAnyDigit = matchNum.pre.stripRight('0');

     pairs.filter!(a => preAnyDigit.length >= a.key.length)
          .filter!(a => a.key == preAnyDigit[$ - a.key.length .. 
$])
          .each!(a => ins = a.value);
}

Jordan



More information about the Digitalmars-d-learn mailing list