mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Merge pull request #1495 from gkotian/master
First set of changes to the D programming page
This commit is contained in:
commit
96bb814889
@ -23,8 +23,10 @@ about [D](http://dlang.org/). The D programming language is a modern, general-pu
|
|||||||
multi-paradigm language with support for everything from low-level features to
|
multi-paradigm language with support for everything from low-level features to
|
||||||
expressive high-level abstractions.
|
expressive high-level abstractions.
|
||||||
|
|
||||||
D is actively developed by Walter Bright and Andrei Alexandrescu, two super smart, really cool
|
D is actively developed by a large group of super-smart people and is spearheaded by
|
||||||
dudes. With all that out of the way, let's look at some examples!
|
[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!
|
||||||
|
|
||||||
```c
|
```c
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
@ -36,9 +38,10 @@ void main() {
|
|||||||
writeln(i);
|
writeln(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto n = 1; // use auto for type inferred variables
|
// 'auto' can be used for inferring types.
|
||||||
|
auto n = 1;
|
||||||
|
|
||||||
// Numeric literals can use _ as a digit seperator for clarity
|
// Numeric literals can use '_' as a digit separator for clarity.
|
||||||
while(n < 10_000) {
|
while(n < 10_000) {
|
||||||
n += n;
|
n += n;
|
||||||
}
|
}
|
||||||
@ -47,13 +50,15 @@ void main() {
|
|||||||
n -= (n / 2);
|
n -= (n / 2);
|
||||||
} while(n > 0);
|
} while(n > 0);
|
||||||
|
|
||||||
// For and while are nice, but in D-land we prefer foreach
|
// For and while are nice, but in D-land we prefer 'foreach' loops.
|
||||||
// The .. creates a continuous range, excluding the end
|
// The '..' creates a continuous range, including the first value
|
||||||
|
// but excluding the last.
|
||||||
foreach(i; 1..1_000_000) {
|
foreach(i; 1..1_000_000) {
|
||||||
if(n % 2 == 0)
|
if(n % 2 == 0)
|
||||||
writeln(i);
|
writeln(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// There's also 'foreach_reverse' when you want to loop backwards.
|
||||||
foreach_reverse(i; 1..int.max) {
|
foreach_reverse(i; 1..int.max) {
|
||||||
if(n % 2 == 1) {
|
if(n % 2 == 1) {
|
||||||
writeln(i);
|
writeln(i);
|
||||||
@ -78,7 +83,7 @@ struct LinkedList(T) {
|
|||||||
class BinTree(T) {
|
class BinTree(T) {
|
||||||
T data = null;
|
T data = null;
|
||||||
|
|
||||||
// If there is only one template parameter, we can omit parens
|
// If there is only one template parameter, we can omit the parentheses
|
||||||
BinTree!T left;
|
BinTree!T left;
|
||||||
BinTree!T right;
|
BinTree!T right;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user