www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here] Rounding real numbers

reply Justin Whear <justin economicmodeling.com> writes:
A process for rounding numbers.  This incarnation can be run like
  round 1.23 3.4 4
or by reading lines from stdin.  It could be simplified as an example by 
getting rid of the argument-processing form.  It shows off templated 
function composition using std.functional.pipe, ct-regexes, and component 
programming.
I wrote this after realizing that there wasn't a convenient UNIX utility 
for doing this and use it regularly.

---------------------------------
import std.algorithm,
    std.conv,
    std.functional,
    std.math,
    std.regex,
    std.stdio;

// Transforms input into a real number, rounds it, then to a string
alias round = pipe!(to!real, lround, to!string);

// Matches numbers that look like they need rounding
static reFloatingPoint = ctRegex!`[0-9]+\.[0-9]+`;

void main(string[] args)
{
    // If arguments, process those and exit, otherwise wait around
    //  for input on stdin
    if (args.length > 1)
        args[1..$].map!round.joiner(" ").writeln;
 
    else
        // Replace anything that looks like a real number with the 
        //  rounded equivalent.
        stdin.byLine(KeepTerminator.yes)
             .map!(l => l.replaceAll!(c => c.hit.round)(reFloatingPoint))
             .copy(stdout.lockingTextWriter());
}
---------------------------------
May 01 2015
next sibling parent reply "Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> writes:
On Friday, 1 May 2015 at 17:17:09 UTC, Justin Whear wrote:
 A process for rounding numbers.
Thanks Justin. Could someone take this? We don't have PHP code for rotating examples randomly yet. -- Andrei
May 02 2015
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Saturday, 2 May 2015 at 22:23:01 UTC, Andrei Alexandrescu 
wrote:
 On Friday, 1 May 2015 at 17:17:09 UTC, Justin Whear wrote:
 A process for rounding numbers.
Thanks Justin. Could someone take this? We don't have PHP code for rotating examples randomly yet. -- Andrei
Doesn't need to be PHP, just show one example statically and switch it with a random one with JS.
May 02 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 3 May 2015 at 02:48:32 UTC, Vladimir Panteleev wrote:
 Doesn't need to be PHP
could also be D
May 02 2015
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 5/2/15 7:51 PM, Adam D. Ruppe wrote:
 On Sunday, 3 May 2015 at 02:48:32 UTC, Vladimir Panteleev wrote:
 Doesn't need to be PHP
could also be D
"code or it didn't happen" -- Andrei
May 02 2015
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 5/2/15 7:48 PM, Vladimir Panteleev wrote:
 On Saturday, 2 May 2015 at 22:23:01 UTC, Andrei Alexandrescu wrote:
 On Friday, 1 May 2015 at 17:17:09 UTC, Justin Whear wrote:
 A process for rounding numbers.
Thanks Justin. Could someone take this? We don't have PHP code for rotating examples randomly yet. -- Andrei
Doesn't need to be PHP, just show one example statically and switch it with a random one with JS.
That'd work. I gather you just volunteered :o). -- Andrei
May 02 2015
parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sunday, 3 May 2015 at 04:14:40 UTC, Andrei Alexandrescu wrote:
 On 5/2/15 7:48 PM, Vladimir Panteleev wrote:
 On Saturday, 2 May 2015 at 22:23:01 UTC, Andrei Alexandrescu 
 wrote:
 On Friday, 1 May 2015 at 17:17:09 UTC, Justin Whear wrote:
 A process for rounding numbers.
Thanks Justin. Could someone take this? We don't have PHP code for rotating examples randomly yet. -- Andrei
Doesn't need to be PHP, just show one example statically and switch it with a random one with JS.
That'd work. I gather you just volunteered :o). -- Andrei
https://github.com/D-Programming-Language/dlang.org/pull/988
May 04 2015
prev sibling parent Justin Whear <justin economicmodeling.com> writes:
Arrrg, formatting got torn up.  Here's a Dpaste:
http://dpaste.dzfl.pl/ca190950f199
May 04 2015