IntroductionΒΆ
It was mentioned that students can quickly master the D programming language without a detailed study using mostly its subset, which is close to the C PL.
Consider a simple program that reads from a file of 10 lines, each containing a single number and prints to the standard output at the same number, but shifted to the mathematician expectation.
Whereas idiomatic D code looks pretty unusual:
import std.algorithm.iteration : map, each;
import std.array : array;
import std.conv : parse;
import std.range : takeExactly, tee;
import std.stdio;
void main()
{
double mean = 0;
auto sample = File("10numbers.txt")
.byLine
.takeExactly(10)
.map!(line => line.parse!double)
.tee!((x){mean += x;})
.array;
mean /= sample.length;
// prints one element per line
sample.map!(x => x - mean).each!writeln;
}
for many unfamiliar with the language D the same program can be implemented even as more complex, but at the same time more understandable way:
import std.stdio;
void main()
{
File fin = File("10numbers.txt");
double[] sample;
sample.length = 10;
double mean = 0;
for(int i = 0; i < sample.length; i++)
{
fin.readf("%s", &sample[i]);
if(!fin.eof)
fin.readln();
mean += sample[i];
}
mean /= sample.length;
// prints one element per line
for(int i = 0; i < sample.length; i++)
{
writeln(sample[i] - mean);
}
}
The present documentation is submitted to the rapid introduction to D for those who are already somehow familiar with the C language and for some reasons do not want to waste time on a consistent study of the D language and related tools.
If you decide to use the D language in your daily work, you should start immediately with the study of the official page and of the book “The D Programming Language” by Andrei Alexandrescu.
Probably D is the most powerful of the present system programming languages.
D is a dragon [1]. Have a nice flight!
[1] | D is a dragon, or why D matters for Bioinformatics by Pjotr Prins. |