summaryrefslogtreecommitdiffhomepage
path: root/d.html.markdown
blob: 0ffe35087ebb75396b132bfe8fe44b95200e2a81 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
language: D 
filename: learnd.d 
contributors:
    - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
lang: en
---

If you're like me and spend way too 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...
module hello;

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; 1..short.max) {
        if(n % 2 == 1) {
            writeln(i);
        } else {
            writeln("No!");
        }
    }
}
```

We can define new types and functions with `struct`, `class`, `union`, and `enum`. Structs and unions
are passed to functions by value (i.e. copied) and classes are passed by reference. Futhermore,
we can use templates to parameterize all of these on both types and values!

```d
// Here, T is a type parameter. Think <T> from C++/C#/Java
struct LinkedList(T) {
    T data = null;
    LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think <T> 
}

class BinTree(T) {
    T data = null;

    BinTree!T left;
    BinTree!T right;
}

enum Day {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

// Use alias to create abbreviations for types

alias IntList = LinkedList!int;
alias NumTree = BinTree!double;

// We can create function templates as well!

T max(T)(T a, T b) {
    if(a < b) 
        return b;

    return a;
}

// Use the ref keyword to pass by referece
void swap(T)(ref T a, ref T b) {
    auto temp = a;

    a = b;
    b = temp; 
}

// With templates, we can also parameterize on values, not just types
class Matrix(uint m, uint n, T = int) {
    T[m] rows;
    T[n] columns;
}

auto mat = new Matrix!(3, 3); // We've defaulted T to int

```

Speaking of classes, let's talk about properties for a second. A property
is roughly a function that may act like an lvalue, so we can
have the syntax of POD structures (`structure.x = 7`) with the semantics of
getter and setter methods (`object.setX(7)`)!

```d
// Consider a class parameterized on a types T, U

class MyClass(T, U) {
    T _data;
    U _other;

}

// We define "setter" methods as follows

class MyClass(T, U) {
    T _data;
    U _other;
    
    @property void data(T t) {
        _data = t;
    }

    @property void other(U u) {
        _other = u;
    }
}

// And "getter" methods like so
class MyClass(T, U) {
    T _data;
    U _other;
    
    // Constructors are always named `this`
    this(T t, U u) {
        data = t;
        other = u;
    }
    
    // getters
    @property T data() {
        return _data;
    }

    @property U other() {
        return _other;
    }

    // setters    
    @property void data(T t) {
        _data = t;
    }

    @property void other(U u) {
        _other = u;
    }
}
// And we use them in this manner

void main() {
    auto mc = MyClass!(int, string);

    mc.data = 7;
    mc.other = "seven";

    writeln(mc.data);
    writeln(mc.other);
}
```

With properties, we can add any amount of validation to
our getter and setter methods, and keep the clean syntax of
accessing members directly!

Other object-oriented goodness for all your enterprise needs
include `interface`s, `abstract class`es,
and `override`ing methods.

We've seen D's OOP facilities, but let's switch gears. D offers
functional programming with first-class functions, `pure` 
functions, and immutable data. In addition, all of your favorite
functional algorithms (map, filter, reduce and friends) can be
found in the wonderful `std.algorithm` module!

```d
import std.algorithm;

void main() {
    // We want to print the sum of a list of squares of even ints
    // from 1 to 100. Easy!

    // Just pass lambda expressions as template parameters!
    auto num = iota(1, 101).filter!(x => x % 2 == 0)
                           .map!(y => y ^^ 2)
                           .reduce!((a, b) => a + b);

    writeln(num);
}
```

Notice how we got to build a nice Haskellian pipeline to compute num? 
That's thanks to a D innovation know as Uniform Function Call Syntax.
With UFCS, we can choose whether to write a function call as a method
or free function all. In general, if we have a function 

```d
f(A, B, C, ...)
```

Then we may write 

```d
A.f(B, C, ...)
```

and the two are equivalent! No more fiddling to remember if it's
str.length or length(str)!