www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to write similar code D?

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
Feb 09 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Feb 09 2015
parent reply FG <home fgda.pl> writes:
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.
Feb 09 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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.
Feb 09 2015
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Feb 10 2015