From ab637d83f235a1766cbfc4c8a1d84cde4a670820 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Sun, 7 Jun 2015 22:30:16 -0400 Subject: Prompt addition of D --- d.html.markdown | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 d.html.markdown (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown new file mode 100644 index 00000000..9f322423 --- /dev/null +++ b/d.html.markdown @@ -0,0 +1,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!"); + } +} + +''' -- cgit v1.2.3 From 8bdf48dc8c2d41f945db734ac1837b0e434e7a95 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Sun, 7 Jun 2015 22:39:01 -0400 Subject: fixed D syntax` --- d.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 9f322423..a6245a99 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -1,5 +1,5 @@ --- -language: d +language: D filename: learnd.d contributors: - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"] @@ -17,7 +17,7 @@ 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 +```d // You know what's coming... import std.stdio; @@ -58,4 +58,4 @@ void main() { } } -''' +``` -- cgit v1.2.3 From c5ac70f706802e9642c7ddc81caf88482a0eb2c6 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Sun, 7 Jun 2015 22:50:05 -0400 Subject: D stuff --- d.html.markdown | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index a6245a99..be9f8fb0 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -50,12 +50,47 @@ void main() { writeln(i); } - foreach_reverse(i; short.max) { + 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! + +``` +// Here, T is a type parameter. Think from C++/C#/Java +struct(T) LinkedList { + T data = null; + LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think +} + +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; ``` -- cgit v1.2.3 From f8da3b96ff93723f5e61aff24da7aa9e1c65f4a8 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Sun, 7 Jun 2015 22:52:34 -0400 Subject: fucking syntax highlighting --- d.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index be9f8fb0..f3836c2c 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -63,7 +63,7 @@ We can define new types and functions with `struct`, `class`, `union`, and `enum 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 from C++/C#/Java struct(T) LinkedList { T data = null; -- cgit v1.2.3 From 8f24f723e0b7cccde9dfcef2e2aa87d7eeb3822b Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 11:54:16 -0400 Subject: more D --- d.html.markdown | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index f3836c2c..10a2be29 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -19,6 +19,8 @@ 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 @@ -65,7 +67,7 @@ we can use templates to parameterize all of these on both types and values! ```d // Here, T is a type parameter. Think from C++/C#/Java -struct(T) LinkedList { +struct LinkedList(T) { T data = null; LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think } @@ -90,7 +92,33 @@ enum Day { // 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 = a; +} + +// With templates, we can also parameterize on values, not just types +class Matrix(T = int, uint m, uint n) { + T[m] rows; + T[n] columns; +} ``` + +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)`)! -- cgit v1.2.3 From e5fa463b3477813bb11d590beaf7ed8b49e9c733 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 12:18:45 -0400 Subject: So much D --- d.html.markdown | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 10a2be29..f547cc56 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -53,10 +53,11 @@ void main() { } foreach_reverse(i; 1..short.max) { - if(n % 2 == 1) + if(n % 2 == 1) { writeln(i); - else + } else { writeln("No!"); + } } } ``` @@ -122,3 +123,73 @@ 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! -- cgit v1.2.3 From cd207d1590897b9d410eb7e91992a6b11d36cd50 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 12:21:11 -0400 Subject: So much D --- d.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index f547cc56..d816a312 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -6,7 +6,7 @@ contributors: lang: en --- -If you're like me and spend way to 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 fantastic support for OOP, functional programming, metaprogramming, and easy concurrency and parallelism, and runs the gamut from low-level features such as @@ -113,10 +113,13 @@ void swap(T)(ref T a, ref T b) { } // With templates, we can also parameterize on values, not just types -class Matrix(T = int, uint m, uint n) { +class Matrix(uint m, uint n, T = int) { T[m] rows; T[n] columns; } + +auto mat = new Matrix!(3, 3); + ``` Speaking of classes, let's talk about properties for a second. A property -- cgit v1.2.3 From cc5729245ff988ebd92fddb6bcb2678be0d64cec Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 12:42:10 -0400 Subject: I looooove the D --- d.html.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index d816a312..0ffe3508 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -109,7 +109,7 @@ void swap(T)(ref T a, ref T b) { auto temp = a; a = b; - b = a; + b = temp; } // With templates, we can also parameterize on values, not just types @@ -118,7 +118,7 @@ class Matrix(uint m, uint n, T = int) { T[n] columns; } -auto mat = new Matrix!(3, 3); +auto mat = new Matrix!(3, 3); // We've defaulted T to int ``` @@ -196,3 +196,47 @@ void main() { 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)! -- cgit v1.2.3 From 455875cd919d829874156ea97887e514e2b06493 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 14:07:14 -0400 Subject: improvements to D --- d.html.markdown | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 0ffe3508..4b993a2d 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -47,12 +47,13 @@ void main() { } 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 + // The .. creates a continuous range, excluding the end + foreach(i; 1..1000000) { if(n % 2 == 0) writeln(i); } - foreach_reverse(i; 1..short.max) { + foreach_reverse(i; 1..int.max) { if(n % 2 == 1) { writeln(i); } else { @@ -62,7 +63,7 @@ void main() { } ``` -We can define new types and functions with `struct`, `class`, `union`, and `enum`. Structs and unions +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, we can use templates to parameterize all of these on both types and values! @@ -75,7 +76,8 @@ struct LinkedList(T) { class BinTree(T) { T data = null; - + + // If there is only one template parameter, we can omit parens BinTree!T left; BinTree!T right; } @@ -104,7 +106,9 @@ T max(T)(T a, T b) { return a; } -// Use the ref keyword to pass by referece +// 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 void swap(T)(ref T a, ref T b) { auto temp = a; @@ -136,22 +140,7 @@ class MyClass(T, U) { } -// 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 +// And "getter" and "setter" methods like so class MyClass(T, U) { T _data; U _other; @@ -197,7 +186,7 @@ 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 +Other object-oriented goodies at our disposal include `interface`s, `abstract class`es, and `override`ing methods. @@ -208,12 +197,13 @@ functional algorithms (map, filter, reduce and friends) can be found in the wonderful `std.algorithm` module! ```d -import std.algorithm; +import std.algorithm : map, filter, reduce; +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! auto num = iota(1, 101).filter!(x => x % 2 == 0) .map!(y => y ^^ 2) -- cgit v1.2.3 From f30876d3f659d2c28ebe97bd4416d4dd514e5d22 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 14:13:38 -0400 Subject: So much D in my life --- d.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 4b993a2d..36153500 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -205,6 +205,7 @@ void main() { // 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. auto num = iota(1, 101).filter!(x => x % 2 == 0) .map!(y => y ^^ 2) .reduce!((a, b) => a + b); -- cgit v1.2.3 From 2db99047ef98ec636b78e29b72f46dc347b37d38 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Wed, 10 Jun 2015 14:31:02 -0400 Subject: Update d.html.markdown --- d.html.markdown | 7 ------- 1 file changed, 7 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 36153500..3a5a6c9b 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -133,13 +133,6 @@ 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; - -} - // And "getter" and "setter" methods like so class MyClass(T, U) { T _data; -- cgit v1.2.3 From 8bb2c3db63b87582764dca2641c8d84b39ed017f Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Tue, 16 Jun 2015 17:10:57 -0400 Subject: UFCS corrections; shorter intro --- d.html.markdown | 53 ++++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 36153500..f0e9ce02 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -6,17 +6,6 @@ contributors: 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; @@ -28,16 +17,27 @@ void main(string[] args) { writeln("Hello, World!"); } -// Conditionals and loops work as expected. +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 +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! + + import std.stdio; void main() { - for(int i = 0; i < 5; i++) { + + // Conditionals and loops work as expected. + for(int i = 0; i < 10000; i++) { writeln(i); } auto n = 1; // use auto for type inferred variables - + + // Numeric literals can use _ as a digit seperator for clarity while(n < 10_000) { n += n; } @@ -48,7 +48,7 @@ void main() { // For and while are nice, but in D-land we prefer foreach // The .. creates a continuous range, excluding the end - foreach(i; 1..1000000) { + foreach(i; 1..1_000_000) { if(n % 2 == 0) writeln(i); } @@ -122,7 +122,7 @@ class Matrix(uint m, uint n, T = int) { T[n] columns; } -auto mat = new Matrix!(3, 3); // We've defaulted T to int +auto mat = new Matrix!(3, 3); // We've defaulted type T to int ``` @@ -176,19 +176,20 @@ void main() { mc.data = 7; mc.other = "seven"; - + writeln(mc.data); writeln(mc.other); } ``` -With properties, we can add any amount of validation to +With properties, we can add any amount of logic to 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. +and `override`ing 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` @@ -217,17 +218,7 @@ 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. 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 +or free function call! Walter wrote a nice article on this [http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394](here.) In short, you can call functions whose first parameter +is of some type A on any expression of type A as a methods. -```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)! -- cgit v1.2.3 From c04bb1a39c0a6feacbff5aaf950a33f8d29f6d79 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Tue, 16 Jun 2015 17:15:14 -0400 Subject: formatting --- d.html.markdown | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index ee06a425..39c6a4d3 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -16,6 +16,7 @@ import std.stdio; void main(string[] args) { writeln("Hello, World!"); } +``` 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, @@ -133,6 +134,13 @@ 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; + +} + // And "getter" and "setter" methods like so class MyClass(T, U) { T _data; @@ -212,6 +220,6 @@ 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 call! Walter wrote a nice article on this [http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394](here.) In short, you can call functions whose first parameter -is of some type A on any expression of type A as a methods. +is of some type A on any expression of type A as a method. -- cgit v1.2.3 From 4df0b5fc697ededd3d1e733214bd94033f4cba53 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Tue, 16 Jun 2015 17:16:24 -0400 Subject: formatting --- d.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 39c6a4d3..4ccf65d3 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -26,7 +26,7 @@ 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 import std.stdio; void main() { -- cgit v1.2.3 From 5f0d8c28cc8c8f33db45c82a79699a9c1ff6fbbc Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Tue, 16 Jun 2015 17:19:30 -0400 Subject: formatting --- d.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 4ccf65d3..7356174d 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -219,7 +219,7 @@ 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. 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 [http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394](here.) In short, you can call functions whose first parameter +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 is of some type A on any expression of type A as a method. -- cgit v1.2.3 From a54c5b580b9a2900f2f6eb1b2b5f07d0c1dd2e9d Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Tue, 16 Jun 2015 17:46:58 -0400 Subject: parallel stuff --- d.html.markdown | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 7356174d..88c7e37f 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -219,7 +219,29 @@ 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. 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 +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 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! +```d +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. + auto arr = new double[1_000_000]; + + // Use an index, and an array element by referece, + // and just call parallel on the array! + foreach(i, ref elem; parallel(arr)) { + ref = sqrt(i + 1.0); + } +} + + +``` -- cgit v1.2.3 From e898c628ef68aaeea82be92ac5b6c5922bbb9f5e Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Mon, 28 Sep 2015 00:10:50 +0800 Subject: Use c highlighting in D article for now. --- d.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 88c7e37f..daba8020 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -6,7 +6,7 @@ contributors: lang: en --- -```d +```c // You know what's coming... module hello; @@ -26,7 +26,7 @@ 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 +```c import std.stdio; void main() { @@ -68,7 +68,7 @@ We can define new types with `struct`, `class`, `union`, and `enum`. Structs and 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 +```c // Here, T is a type parameter. Think from C++/C#/Java struct LinkedList(T) { T data = null; @@ -132,7 +132,7 @@ 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 +```c // Consider a class parameterized on a types T, U class MyClass(T, U) { @@ -198,7 +198,7 @@ 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 +```c import std.algorithm : map, filter, reduce; import std.range : iota; // builds an end-exclusive range @@ -226,7 +226,7 @@ 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! -```d +```c import std.stdio; import std.parallelism : parallel; import std.math : sqrt; -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- d.html.markdown | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index daba8020..ba24b60f 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,9 +18,9 @@ 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 @@ -37,7 +37,7 @@ void main() { } auto n = 1; // use auto for type inferred variables - + // Numeric literals can use _ as a digit seperator for clarity while(n < 10_000) { n += n; @@ -49,7 +49,7 @@ void main() { // 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) { + foreach(i; 1..1_000_000) { if(n % 2 == 0) writeln(i); } @@ -72,12 +72,12 @@ we can use templates to parameterize all of these on both types and values! // Here, T is a type parameter. Think from C++/C#/Java struct LinkedList(T) { T data = null; - LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think + LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think } class BinTree(T) { T data = null; - + // If there is only one template parameter, we can omit parens BinTree!T left; BinTree!T right; @@ -101,7 +101,7 @@ 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; @@ -114,7 +114,7 @@ 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 @@ -145,13 +145,13 @@ class MyClass(T, U) { 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; @@ -161,7 +161,7 @@ class MyClass(T, U) { return _other; } - // setters + // setters @property void data(T t) { _data = t; } @@ -177,7 +177,7 @@ void main() { mc.data = 7; mc.other = "seven"; - + writeln(mc.data); writeln(mc.other); } @@ -193,7 +193,7 @@ and `override`ing 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,7 +205,7 @@ 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. auto num = iota(1, 101).filter!(x => x % 2 == 0) @@ -216,12 +216,12 @@ void main() { } ``` -Notice how we got to build a nice Haskellian pipeline to compute 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 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! -- cgit v1.2.3 From a4d9115decdfaa0346c66994df372e529d343d37 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Tue, 13 Oct 2015 18:15:49 +0200 Subject: Clarify that not just two people develop D The present construction seems to imply that only Walter and Andrei and involved in the development of D. This is nowhere close to being true! --- d.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index ba24b60f..7c23f2dd 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -23,8 +23,8 @@ 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 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 +and Andrei Alexandrescu. With all that out of the way, let's look at some examples! ```c import std.stdio; -- cgit v1.2.3 From 064b82eab443fa1bc8c1dd0b061bedbc04b60e66 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Tue, 13 Oct 2015 18:16:30 +0200 Subject: Add wikipedia page links Link to the wikipedia pages of Walter Bright and Andrei Alexandrescu in an attempt to at least partially establish their credibility. --- d.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 7c23f2dd..d56e08a6 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -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 expressive high-level abstractions. -D is actively developed by a large group of super-smart people and is spearheaded by Walter Bright -and Andrei Alexandrescu. 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; -- cgit v1.2.3 From 4be1044a64e7ac1000a458087ee9131a9999d05f Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Tue, 13 Oct 2015 18:17:11 +0200 Subject: Improve code comments --- d.html.markdown | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index d56e08a6..88a83e41 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -38,9 +38,10 @@ void main() { 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) { n += n; } @@ -49,13 +50,15 @@ 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 + // 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(i; 1..1_000_000) { if(n % 2 == 0) writeln(i); } + // There's also 'foreach_reverse' when you want to loop backwards. foreach_reverse(i; 1..int.max) { if(n % 2 == 1) { writeln(i); @@ -80,7 +83,7 @@ struct LinkedList(T) { 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; } -- cgit v1.2.3 From d136abe95434a94bd70a981b1ea2bf0ee1dbf636 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Thu, 15 Oct 2015 07:31:48 +0200 Subject: Improve code comments --- d.html.markdown | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 88a83e41..ea1c1700 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -74,16 +74,18 @@ are passed to functions by value (i.e. copied) and classes are passed by referen we can use templates to parameterize all of these on both types and values! ```c -// Here, T is a type parameter. Think from C++/C#/Java +// Here, 'T' is a type parameter. Think '' from C++/C#/Java. struct LinkedList(T) { T data = null; - LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think + + // Use '!' to instantiate a parameterized type. Again, think ''. + LinkedList!(T)* next; } class BinTree(T) { T data = null; - // If there is only one template parameter, we can omit the parentheses + // If there is only one template parameter, we can omit the parentheses. BinTree!T left; BinTree!T right; } @@ -98,13 +100,11 @@ 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) return b; @@ -112,9 +112,8 @@ T max(T)(T a, T 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; @@ -122,13 +121,13 @@ void swap(T)(ref T a, ref T b) { 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'. ``` @@ -138,21 +137,20 @@ 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; } @@ -175,8 +173,8 @@ 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); -- cgit v1.2.3 From a77d0264c9df6ac2ac4c1e2202e352704580c579 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Thu, 15 Oct 2015 07:32:48 +0200 Subject: Make the code compilable and add some additional comments to explain what's happening. --- d.html.markdown | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index ea1c1700..80c1dc65 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -176,13 +176,21 @@ class MyClass(T, U) { // And we use them in this manner: void main() { - auto mc = MyClass!(int, string); + auto mc = new MyClass!(int, string)(7, "seven"); - mc.data = 7; - mc.other = "seven"; + // Import the 'stdio' module from the standard library for writing to + // console (imports can be local to a scope). + import std.stdio; - writeln(mc.data); - writeln(mc.other); + // 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); } ``` -- cgit v1.2.3 From 9aad08bf78196758a568ec1272c94029221c2dd5 Mon Sep 17 00:00:00 2001 From: Amru Eliwat Date: Fri, 23 Oct 2015 23:13:29 -0700 Subject: Fixed typos for 'overriding' and 'reference' and fixed formatting issue that may have caused confusion --- d.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 80c1dc65..6f3710ab 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -199,8 +199,8 @@ 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 @@ -247,7 +247,7 @@ void main() { // and take advantage of as many cores as we have available. auto arr = new double[1_000_000]; - // Use an index, and an array element by referece, + // Use an index, and an array element by reference, // and just call parallel on the array! foreach(i, ref elem; parallel(arr)) { ref = sqrt(i + 1.0); -- cgit v1.2.3