2015-06-08 05:30:16 +03:00
|
|
|
---
|
2015-10-08 06:11:24 +03:00
|
|
|
language: D
|
|
|
|
filename: learnd.d
|
2015-06-08 05:30:16 +03:00
|
|
|
contributors:
|
|
|
|
- ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
|
2017-08-25 11:40:11 +03:00
|
|
|
|
2015-06-08 05:30:16 +03:00
|
|
|
---
|
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```d
|
2015-06-08 05:30:16 +03:00
|
|
|
// You know what's coming...
|
2015-06-10 18:54:16 +03:00
|
|
|
module hello;
|
|
|
|
|
2015-06-08 05:30:16 +03:00
|
|
|
import std.stdio;
|
|
|
|
|
|
|
|
// args is optional
|
|
|
|
void main(string[] args) {
|
|
|
|
writeln("Hello, World!");
|
|
|
|
}
|
2015-06-17 00:15:14 +03:00
|
|
|
```
|
2015-06-08 05:30:16 +03:00
|
|
|
|
2015-10-08 06:11:24 +03:00
|
|
|
If you're like me and spend way too much time on the internet, odds are you've heard
|
2015-06-17 00:10:57 +03:00
|
|
|
about [D](http://dlang.org/). The D programming language is a modern, general-purpose,
|
2015-10-08 06:11:24 +03:00
|
|
|
multi-paradigm language with support for everything from low-level features to
|
2015-06-17 00:10:57 +03:00
|
|
|
expressive high-level abstractions.
|
|
|
|
|
2015-10-13 19:16:30 +03:00
|
|
|
D is actively developed by a large group of super-smart people and is spearheaded by
|
|
|
|
[Walter Bright](https://en.wikipedia.org/wiki/Walter_Bright) and
|
|
|
|
[Andrei Alexandrescu](https://en.wikipedia.org/wiki/Andrei_Alexandrescu).
|
|
|
|
With all that out of the way, let's look at some examples!
|
2015-06-17 00:10:57 +03:00
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```d
|
2015-06-08 05:30:16 +03:00
|
|
|
import std.stdio;
|
|
|
|
|
|
|
|
void main() {
|
2015-06-17 00:10:57 +03:00
|
|
|
|
|
|
|
// Conditionals and loops work as expected.
|
|
|
|
for(int i = 0; i < 10000; i++) {
|
2015-06-08 05:30:16 +03:00
|
|
|
writeln(i);
|
|
|
|
}
|
|
|
|
|
2015-10-13 19:17:11 +03:00
|
|
|
// 'auto' can be used for inferring types.
|
|
|
|
auto n = 1;
|
2015-10-08 06:11:24 +03:00
|
|
|
|
2015-10-13 19:17:11 +03:00
|
|
|
// Numeric literals can use '_' as a digit separator for clarity.
|
2015-06-08 05:30:16 +03:00
|
|
|
while(n < 10_000) {
|
|
|
|
n += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
n -= (n / 2);
|
|
|
|
} while(n > 0);
|
|
|
|
|
2015-10-13 19:17:11 +03:00
|
|
|
// For and while are nice, but in D-land we prefer 'foreach' loops.
|
|
|
|
// The '..' creates a continuous range, including the first value
|
|
|
|
// but excluding the last.
|
2015-11-11 10:31:16 +03:00
|
|
|
foreach(n; 1..1_000_000) {
|
2015-06-08 05:30:16 +03:00
|
|
|
if(n % 2 == 0)
|
2015-11-11 10:31:16 +03:00
|
|
|
writeln(n);
|
2015-06-08 05:30:16 +03:00
|
|
|
}
|
|
|
|
|
2015-10-13 19:17:11 +03:00
|
|
|
// There's also 'foreach_reverse' when you want to loop backwards.
|
2015-11-11 10:31:16 +03:00
|
|
|
foreach_reverse(n; 1..int.max) {
|
2015-06-10 19:18:45 +03:00
|
|
|
if(n % 2 == 1) {
|
2015-11-11 10:31:16 +03:00
|
|
|
writeln(n);
|
2015-06-10 19:18:45 +03:00
|
|
|
} else {
|
2015-06-08 05:30:16 +03:00
|
|
|
writeln("No!");
|
2015-06-10 19:18:45 +03:00
|
|
|
}
|
2015-06-08 05:30:16 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 05:50:05 +03:00
|
|
|
```
|
|
|
|
|
2015-06-10 21:07:14 +03:00
|
|
|
We can define new types with `struct`, `class`, `union`, and `enum`. Structs and unions
|
2015-10-23 07:55:00 +03:00
|
|
|
are passed to functions by value (i.e. copied) and classes are passed by reference. Furthermore,
|
2015-06-08 05:50:05 +03:00
|
|
|
we can use templates to parameterize all of these on both types and values!
|
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```d
|
2015-10-15 08:31:48 +03:00
|
|
|
// Here, 'T' is a type parameter. Think '<T>' from C++/C#/Java.
|
2015-06-10 18:54:16 +03:00
|
|
|
struct LinkedList(T) {
|
2015-06-08 05:50:05 +03:00
|
|
|
T data = null;
|
2015-10-15 08:31:48 +03:00
|
|
|
|
|
|
|
// Use '!' to instantiate a parameterized type. Again, think '<T>'.
|
|
|
|
LinkedList!(T)* next;
|
2015-06-08 05:50:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class BinTree(T) {
|
|
|
|
T data = null;
|
2015-10-08 06:11:24 +03:00
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// If there is only one template parameter, we can omit the parentheses.
|
2015-06-08 05:50:05 +03:00
|
|
|
BinTree!T left;
|
|
|
|
BinTree!T right;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Day {
|
|
|
|
Sunday,
|
|
|
|
Monday,
|
|
|
|
Tuesday,
|
|
|
|
Wednesday,
|
|
|
|
Thursday,
|
|
|
|
Friday,
|
|
|
|
Saturday,
|
|
|
|
}
|
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// Use alias to create abbreviations for types.
|
2015-06-08 05:50:05 +03:00
|
|
|
alias IntList = LinkedList!int;
|
|
|
|
alias NumTree = BinTree!double;
|
2015-06-08 05:30:16 +03:00
|
|
|
|
2015-06-10 18:54:16 +03:00
|
|
|
// We can create function templates as well!
|
|
|
|
T max(T)(T a, T b) {
|
2015-10-08 06:11:24 +03:00
|
|
|
if(a < b)
|
2015-06-10 18:54:16 +03:00
|
|
|
return b;
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// Use the ref keyword to ensure pass by reference. That is, even if 'a' and 'b'
|
|
|
|
// are value types, they will always be passed by reference to 'swap()'.
|
2015-06-10 18:54:16 +03:00
|
|
|
void swap(T)(ref T a, ref T b) {
|
|
|
|
auto temp = a;
|
|
|
|
|
|
|
|
a = b;
|
2015-10-08 06:11:24 +03:00
|
|
|
b = temp;
|
2015-06-10 18:54:16 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// With templates, we can also parameterize on values, not just types.
|
2015-06-10 19:21:11 +03:00
|
|
|
class Matrix(uint m, uint n, T = int) {
|
2015-06-10 18:54:16 +03:00
|
|
|
T[m] rows;
|
|
|
|
T[n] columns;
|
|
|
|
}
|
2015-06-10 19:21:11 +03:00
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
auto mat = new Matrix!(3, 3); // We've defaulted type 'T' to 'int'.
|
2015-06-08 05:39:01 +03:00
|
|
|
```
|
2015-06-10 18:54:16 +03:00
|
|
|
|
|
|
|
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)`)!
|
2015-06-10 19:18:45 +03:00
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```d
|
2015-10-15 08:31:48 +03:00
|
|
|
// Consider a class parameterized on types 'T' & 'U'.
|
2015-06-17 00:15:14 +03:00
|
|
|
class MyClass(T, U) {
|
|
|
|
T _data;
|
|
|
|
U _other;
|
|
|
|
}
|
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// And "getter" and "setter" methods like so:
|
2015-06-10 19:18:45 +03:00
|
|
|
class MyClass(T, U) {
|
|
|
|
T _data;
|
|
|
|
U _other;
|
2015-10-08 06:11:24 +03:00
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// Constructors are always named 'this'.
|
2015-06-10 19:18:45 +03:00
|
|
|
this(T t, U u) {
|
2015-10-15 08:31:48 +03:00
|
|
|
// This will call the setter methods below.
|
2015-06-10 19:18:45 +03:00
|
|
|
data = t;
|
|
|
|
other = u;
|
|
|
|
}
|
2015-10-08 06:11:24 +03:00
|
|
|
|
2015-06-10 19:18:45 +03:00
|
|
|
// getters
|
|
|
|
@property T data() {
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@property U other() {
|
|
|
|
return _other;
|
|
|
|
}
|
|
|
|
|
2015-10-08 06:11:24 +03:00
|
|
|
// setters
|
2015-06-10 19:18:45 +03:00
|
|
|
@property void data(T t) {
|
|
|
|
_data = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
@property void other(U u) {
|
|
|
|
_other = u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 08:31:48 +03:00
|
|
|
// And we use them in this manner:
|
2015-06-10 19:18:45 +03:00
|
|
|
void main() {
|
2015-10-15 08:32:48 +03:00
|
|
|
auto mc = new MyClass!(int, string)(7, "seven");
|
2015-06-10 19:18:45 +03:00
|
|
|
|
2015-10-15 08:32:48 +03:00
|
|
|
// Import the 'stdio' module from the standard library for writing to
|
|
|
|
// console (imports can be local to a scope).
|
|
|
|
import std.stdio;
|
2015-10-08 06:11:24 +03:00
|
|
|
|
2015-10-15 08:32:48 +03:00
|
|
|
// Call the getters to fetch the values.
|
|
|
|
writefln("Earlier: data = %d, str = %s", mc.data, mc.other);
|
|
|
|
|
|
|
|
// Call the setters to assign new values.
|
|
|
|
mc.data = 8;
|
|
|
|
mc.other = "eight";
|
|
|
|
|
|
|
|
// Call the getters again to fetch the new values.
|
|
|
|
writefln("Later: data = %d, str = %s", mc.data, mc.other);
|
2015-06-10 19:18:45 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-06-17 00:10:57 +03:00
|
|
|
With properties, we can add any amount of logic to
|
2015-06-10 19:18:45 +03:00
|
|
|
our getter and setter methods, and keep the clean syntax of
|
|
|
|
accessing members directly!
|
2015-06-10 19:42:10 +03:00
|
|
|
|
2015-06-10 21:07:14 +03:00
|
|
|
Other object-oriented goodies at our disposal
|
2015-10-24 09:13:29 +03:00
|
|
|
include interfaces, abstract classes,
|
|
|
|
and overriding methods. D does inheritance just like Java:
|
2015-06-17 00:10:57 +03:00
|
|
|
Extend one class, implement as many interfaces as you please.
|
2015-06-10 19:42:10 +03:00
|
|
|
|
|
|
|
We've seen D's OOP facilities, but let's switch gears. D offers
|
2015-10-08 06:11:24 +03:00
|
|
|
functional programming with first-class functions, `pure`
|
2015-06-10 19:42:10 +03:00
|
|
|
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!
|
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```d
|
2015-06-10 21:07:14 +03:00
|
|
|
import std.algorithm : map, filter, reduce;
|
|
|
|
import std.range : iota; // builds an end-exclusive range
|
2020-01-22 15:16:26 +03:00
|
|
|
import std.stdio;
|
2015-06-10 19:42:10 +03:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
// We want to print the sum of a list of squares of even ints
|
|
|
|
// from 1 to 100. Easy!
|
2015-10-08 06:11:24 +03:00
|
|
|
|
2015-06-10 19:42:10 +03:00
|
|
|
// Just pass lambda expressions as template parameters!
|
2015-12-02 22:59:12 +03:00
|
|
|
// You can pass any function you like, but lambdas are convenient here.
|
2015-06-10 19:42:10 +03:00
|
|
|
auto num = iota(1, 101).filter!(x => x % 2 == 0)
|
|
|
|
.map!(y => y ^^ 2)
|
|
|
|
.reduce!((a, b) => a + b);
|
|
|
|
|
|
|
|
writeln(num);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-10-08 06:11:24 +03:00
|
|
|
Notice how we got to build a nice Haskellian pipeline to compute num?
|
2015-12-02 22:59:12 +03:00
|
|
|
That's thanks to a D innovation know as Uniform Function Call Syntax (UFCS).
|
2015-06-10 19:42:10 +03:00
|
|
|
With UFCS, we can choose whether to write a function call as a method
|
2015-06-17 00:46:58 +03:00
|
|
|
or free function call! Walter wrote a nice article on this
|
2015-10-08 06:11:24 +03:00
|
|
|
[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
|
|
|
|
In short, you can call functions whose first parameter
|
2015-06-17 00:15:14 +03:00
|
|
|
is of some type A on any expression of type A as a method.
|
2015-06-10 19:42:10 +03:00
|
|
|
|
2015-06-17 00:46:58 +03:00
|
|
|
I like parallelism. Anyone else like parallelism? Sure you do. Let's do some!
|
2015-06-10 19:42:10 +03:00
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```d
|
2015-12-02 22:59:12 +03:00
|
|
|
// Let's say we want to populate a large array with the square root of all
|
|
|
|
// consecutive integers starting from 1 (up until the size of the array), and we
|
|
|
|
// want to do this concurrently taking advantage of as many cores as we have
|
|
|
|
// available.
|
|
|
|
|
2015-06-17 00:46:58 +03:00
|
|
|
import std.stdio;
|
|
|
|
import std.parallelism : parallel;
|
|
|
|
import std.math : sqrt;
|
|
|
|
|
|
|
|
void main() {
|
2015-12-02 22:59:12 +03:00
|
|
|
// Create your large array
|
2015-06-17 00:46:58 +03:00
|
|
|
auto arr = new double[1_000_000];
|
|
|
|
|
2015-12-02 22:59:12 +03:00
|
|
|
// Use an index, access every array element by reference (because we're
|
|
|
|
// going to change each element) and just call parallel on the array!
|
2015-06-17 00:46:58 +03:00
|
|
|
foreach(i, ref elem; parallel(arr)) {
|
2016-01-02 19:43:19 +03:00
|
|
|
elem = sqrt(i + 1.0);
|
2015-06-17 00:46:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|