On Thursday, 13 October 2016 at 19:06:26 UTC, Karabuta wrote:
    // Replace anything that looks like a real
    // number with the rounded equivalent.
    stdin
        .byLine
        .map!(l => l.replaceAll!(c => c.hit.round)
                                (reFloatingPoint))
        .each!writeln;


I think this example is a bit awkward for D newbies to decipher. I think here we are showing D's ctRegex; dropping the functional map and lambdas would make this more universally understood. Using matchAll rather than replaceAll makes the example a bit simpler too:

// Print a list of rounded decimal numbers
import std.conv, std.functional, std.math, std.regex;

// Chain functions together to round a string parsed as a real number
alias roundString = pipe!(to!real, std.math.round, to!string);

// Create a compile-time Regular Expression to match decimal numbers
static decimalRegex = ctRegex!`[0-9]+\.[0-9]+`;

void main()
{
    import std.stdio;

    // Iterate standard input lines
    foreach (line; stdin.byLine())
    {
        // Find all decimal matches
        foreach (m; line.matchAll(decimalRegex))
        {
m.hit.roundString().writeln(); // round number and print
        }
    }
}


Reply via email to