1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
---
language: d
filename: learnd.d
contributors:
- ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
lang: en
---
If you're like me and spend way to much time on the internet, odds are you've heard
about [D](http://dlang.org/). The D programming language is a modern, general-purpose,
multi-paradigm language with fantastic support for OOP, functional programming, metaprogramming,
and easy concurrency and parallelism, and runs the gamut from low-level features such as
memory management, inline assembly, and pointer arithmetic, to high-level constructs
such as higher-order functions and generic structures and functions via templates, all with
a pleasant syntax, and blazing fast performance!
D is actively developed by Walter Bright and Andrei Alexandrescu, two super smart, really cool
dudes. With all that out of the way, let's look at some examples!
'''d
// You know what's coming...
import std.stdio;
// args is optional
void main(string[] args) {
writeln("Hello, World!");
}
// Conditionals and loops work as expected.
import std.stdio;
void main() {
for(int i = 0; i < 5; i++) {
writeln(i);
}
auto n = 1; // use auto for type inferred variables
while(n < 10_000) {
n += n;
}
do {
n -= (n / 2);
} while(n > 0);
// For and while are nice, but in D-land we prefer foreach
foreach(i; 1..int.max) { // The .. creates a continuous range
if(n % 2 == 0)
writeln(i);
}
foreach_reverse(i; short.max) {
if(n % 2 == 1)
writeln(i);
else
writeln("No!");
}
}
'''
|