Thread overview
How to write similar code D?
Feb 10, 2015
Dennis Ritchie
Feb 10, 2015
bearophile
Feb 10, 2015
FG
Feb 10, 2015
Dennis Ritchie
Feb 10, 2015
bearophile
February 10, 2015
Tell me, please, how to write similar С# code D:

using System;
using System.Linq;

public class Test
{
    public static void Main()
    {
        var query = Enumerable.Range(2, 10)
        .Select(c => new { Length = 2 * c, Height = c * c - 1, Hypotenuse = c * c + 1 })
        .Select(x => string.Format("{0,4}{1,4}{2,4}", x.Height, x.Hypotenuse, x.Length));

        foreach (var x in query)
            Console.WriteLine(x);
    }
}

Output:

  3   5   4
  8  10   6
 15  17   8
 24  26  10
 35  37  12
 48  50  14
 63  65  16
 80  82  18
 99 101  20
120 122  22
February 10, 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
February 10, 2015
On 2015-02-10 at 01:41, bearophile wrote:
>
>      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));
>

I took advantage of the fact that all elements were of the same type:

    auto query = iota(2, 2 + 10)
    .map!(c => ["Length": 2 * c, "Height": c * c - 1, "Hypotenuse": c * c + 1])
    .map!(x => format("%4d%4d%4d", x["Height"], x["Hypotenuse"], x["Length"]));
    ...

and was surprised that it worked straight away. :)
But definitely this looks better and less complicated:

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

It's the foreach version, since `each` isn't officially out yet.
February 10, 2015
On Tuesday, 10 February 2015 at 01:31:54 UTC, FG wrote:
> On 2015-02-10 at 01:41, bearophile wrote:
>>
>>     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));
>>
>
> I took advantage of the fact that all elements were of the same type:
>
>     auto query = iota(2, 2 + 10)
>     .map!(c => ["Length": 2 * c, "Height": c * c - 1, "Hypotenuse": c * c + 1])
>     .map!(x => format("%4d%4d%4d", x["Height"], x["Hypotenuse"], x["Length"]));
>     ...
>
> and was surprised that it worked straight away. :)
> But definitely this looks better and less complicated:
>
>     auto query = iota(2, 12)
>         .map!(c => tuple(c ^^ 2 - 1, c ^^ 2 + 1, 2 * c));
>     foreach (x; query)
>         writefln("%4d%4d%4d", x[]);
>
> It's the foreach version, since `each` isn't officially out yet.

Thank you.
February 10, 2015
FG:

>     auto query = iota(2, 2 + 10)
>     .map!(c => ["Length": 2 * c, "Height": c * c - 1, "Hypotenuse": c * c + 1])
>     .map!(x => format("%4d%4d%4d", x["Height"],

Unlike other languages like JavaScript, the D front-end is very weak in optimizing well such kind of code... I think D compilers handle built-in associative arrays in a very straight way.

Bye,
bearophile