How to write similar code D?

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 9 16:41:56 PST 2015


Dennis Ritchie:

> Tell me, please, how to write similar ะก# code D:

This is more or less exactly the same:

void main() {
     import std.stdio, std.range, std.algorithm, std.typecons, 
std.format;

     auto query = iota(2, 12)
                  .map!(c => Tuple!(int,"length", int,"height", 
int,"hypotenuse")
                                   (2 * c, c ^^ 2 - 1, c ^^ 2 + 1))
                  .map!(x => "%3d%4d%4d".format(x.height, 
x.hypotenuse, x.length));

     foreach (immutable x; query)
         x.writeln;
}


But often you write something more like this in D using the 
latest version of the compiler:


void main() {
     import std.stdio, std.range, std.algorithm, std.typecons;

     iota(2, 12)
     .map!(c => tuple(c ^^ 2 - 1, c ^^ 2 + 1, 2 * c))
     .each!(x => writefln("%3d%4d%4d", x[]));
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list