digitalmars.D.learn - How to write similar code D?
- Dennis Ritchie (27/27) Feb 09 2015 Tell me, please, how to write similar ะก# code D:
- bearophile (24/25) Feb 09 2015 This is more or less exactly the same:
- FG (13/17) Feb 09 2015 I took advantage of the fact that all elements were of the same type:
- Dennis Ritchie (2/27) Feb 09 2015 Thank you.
- bearophile (6/10) Feb 10 2015 Unlike other languages like JavaScript, the D front-end is very
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
Dennis Ritchie: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
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
On Tuesday, 10 February 2015 at 01:31:54 UTC, FG wrote:On 2015-02-10 at 01:41, bearophile wrote:Thank you.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
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









"Dennis Ritchie" <dennis.ritchie mail.ru> 