summaryrefslogtreecommitdiffhomepage
path: root/d.html.markdown
diff options
context:
space:
mode:
authorAayush Ranaut <aayush.ranaut@gmail.com>2015-12-05 11:10:16 +0530
committerAayush Ranaut <aayush.ranaut@gmail.com>2015-12-05 11:10:16 +0530
commitdc675a47edaeced79e13bf99d120c195a38b9ecf (patch)
treee626142c07fa41695b959b606d4337929c9669ed /d.html.markdown
parent0049a475edba88f6537b2490ca9506df23b46368 (diff)
parentc8475eacd742a1c8c6340121aa95f32f65421113 (diff)
Merged and removed confusing comments in python
Diffstat (limited to 'd.html.markdown')
-rw-r--r--d.html.markdown133
1 files changed, 73 insertions, 60 deletions
diff --git a/d.html.markdown b/d.html.markdown
index daba8020..6f88cf84 100644
--- a/d.html.markdown
+++ b/d.html.markdown
@@ -1,6 +1,6 @@
---
-language: D
-filename: learnd.d
+language: D
+filename: learnd.d
contributors:
- ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
lang: en
@@ -18,13 +18,15 @@ void main(string[] args) {
}
```
-If you're like me and spend way too much time on the internet, odds are you've heard
+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 support for everything from low-level features to
+multi-paradigm language with support for everything from low-level features to
expressive high-level abstractions.
-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 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!
```c
import std.stdio;
@@ -36,9 +38,10 @@ void main() {
writeln(i);
}
- auto n = 1; // use auto for type inferred variables
-
- // Numeric literals can use _ as a digit seperator for clarity
+ // 'auto' can be used for inferring types.
+ auto n = 1;
+
+ // Numeric literals can use '_' as a digit separator for clarity.
while(n < 10_000) {
n += n;
}
@@ -47,16 +50,18 @@ void main() {
n -= (n / 2);
} while(n > 0);
- // For and while are nice, but in D-land we prefer foreach
- // The .. creates a continuous range, excluding the end
- foreach(i; 1..1_000_000) {
+ // 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.
+ foreach(n; 1..1_000_000) {
if(n % 2 == 0)
- writeln(i);
+ writeln(n);
}
- foreach_reverse(i; 1..int.max) {
+ // There's also 'foreach_reverse' when you want to loop backwards.
+ foreach_reverse(n; 1..int.max) {
if(n % 2 == 1) {
- writeln(i);
+ writeln(n);
} else {
writeln("No!");
}
@@ -65,20 +70,22 @@ void main() {
```
We can define new types 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,
+are passed to functions by value (i.e. copied) and classes are passed by reference. Furthermore,
we can use templates to parameterize all of these on both types and values!
```c
-// Here, T is a type parameter. Think <T> from C++/C#/Java
+// 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>
+
+ // Use '!' to instantiate a parameterized type. Again, think '<T>'.
+ LinkedList!(T)* next;
}
class BinTree(T) {
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 right;
}
@@ -93,37 +100,34 @@ enum Day {
Saturday,
}
-// Use alias to create abbreviations for types
-
+// 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)
+ if(a < b)
return b;
return a;
}
-// Use the ref keyword to ensure pass by referece.
-// That is, even if a and b are value types, they
-// will always be passed by reference to swap
+// 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()'.
void swap(T)(ref T a, ref T b) {
auto temp = a;
a = b;
- b = temp;
+ b = temp;
}
-// With templates, we can also parameterize on values, not just types
+// 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 type T to int
+auto mat = new Matrix!(3, 3); // We've defaulted type 'T' to 'int'.
```
@@ -133,25 +137,24 @@ have the syntax of POD structures (`structure.x = 7`) with the semantics of
getter and setter methods (`object.setX(7)`)!
```c
-// Consider a class parameterized on a types T, U
-
+// Consider a class parameterized on types 'T' & 'U'.
class MyClass(T, U) {
T _data;
U _other;
-
}
-// And "getter" and "setter" methods like so
+// And "getter" and "setter" methods like so:
class MyClass(T, U) {
T _data;
U _other;
-
- // Constructors are always named `this`
+
+ // Constructors are always named 'this'.
this(T t, U u) {
+ // This will call the setter methods below.
data = t;
other = u;
}
-
+
// getters
@property T data() {
return _data;
@@ -161,7 +164,7 @@ class MyClass(T, U) {
return _other;
}
- // setters
+ // setters
@property void data(T t) {
_data = t;
}
@@ -170,16 +173,24 @@ class MyClass(T, U) {
_other = u;
}
}
-// And we use them in this manner
+// And we use them in this manner:
void main() {
- auto mc = MyClass!(int, string);
+ auto mc = new MyClass!(int, string)(7, "seven");
+
+ // Import the 'stdio' module from the standard library for writing to
+ // console (imports can be local to a scope).
+ import std.stdio;
+
+ // 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";
- mc.data = 7;
- mc.other = "seven";
-
- writeln(mc.data);
- writeln(mc.other);
+ // Call the getters again to fetch the new values.
+ writefln("Later: data = %d, str = %s", mc.data, mc.other);
}
```
@@ -188,12 +199,12 @@ our getter and setter methods, and keep the clean syntax of
accessing members directly!
Other object-oriented goodies at our disposal
-include `interface`s, `abstract class`es,
-and `override`ing methods. D does inheritance just like Java:
+include interfaces, abstract classes,
+and overriding methods. D does inheritance just like Java:
Extend one class, implement as many interfaces as you please.
We've seen D's OOP facilities, but let's switch gears. D offers
-functional programming with first-class functions, `pure`
+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!
@@ -205,9 +216,9 @@ import std.range : iota; // builds an end-exclusive range
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!
- // You can pass any old function you like, but lambdas are convenient here.
+ // You can pass any function you like, but lambdas are convenient here.
auto num = iota(1, 101).filter!(x => x % 2 == 0)
.map!(y => y ^^ 2)
.reduce!((a, b) => a + b);
@@ -216,32 +227,34 @@ void main() {
}
```
-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.
+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 (UFCS).
With UFCS, we can choose whether to write a function call as a method
or free function call! Walter wrote a nice article on this
-[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
-In short, you can call functions whose first parameter
+[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
+In short, you can call functions whose first parameter
is of some type A on any expression of type A as a method.
I like parallelism. Anyone else like parallelism? Sure you do. Let's do some!
```c
+// 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.
+
import std.stdio;
import std.parallelism : parallel;
import std.math : sqrt;
void main() {
- // We want take the square root every number in our array,
- // and take advantage of as many cores as we have available.
+ // Create your large array
auto arr = new double[1_000_000];
- // Use an index, and an array element by referece,
- // and just call parallel on the array!
+ // Use an index, access every array element by reference (because we're
+ // going to change each element) and just call parallel on the array!
foreach(i, ref elem; parallel(arr)) {
ref = sqrt(i + 1.0);
}
}
-
-
```