improvements to D

This commit is contained in:
NickPapanastasiou 2015-06-10 14:07:14 -04:00
parent cc5729245f
commit 455875cd91

View File

@ -47,12 +47,13 @@ void main() {
} 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
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) if(n % 2 == 0)
writeln(i); writeln(i);
} }
foreach_reverse(i; 1..short.max) { foreach_reverse(i; 1..int.max) {
if(n % 2 == 1) { if(n % 2 == 1) {
writeln(i); writeln(i);
} else { } 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, 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! we can use templates to parameterize all of these on both types and values!
@ -75,7 +76,8 @@ 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
BinTree!T left; BinTree!T left;
BinTree!T right; BinTree!T right;
} }
@ -104,7 +106,9 @@ T max(T)(T a, T b) {
return a; 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) { void swap(T)(ref T a, ref T b) {
auto temp = a; auto temp = a;
@ -136,22 +140,7 @@ class MyClass(T, U) {
} }
// We define "setter" methods as follows // And "getter" and "setter" methods like so
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) { class MyClass(T, U) {
T _data; T _data;
U _other; 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 our getter and setter methods, and keep the clean syntax of
accessing members directly! 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, include `interface`s, `abstract class`es,
and `override`ing methods. and `override`ing methods.
@ -208,12 +197,13 @@ functional algorithms (map, filter, reduce and friends) can be
found in the wonderful `std.algorithm` module! found in the wonderful `std.algorithm` module!
```d ```d
import std.algorithm; import std.algorithm : map, filter, reduce;
import std.range : iota; // builds an end-exclusive range
void main() { void main() {
// We want to print the sum of a list of squares of even ints // We want to print the sum of a list of squares of even ints
// from 1 to 100. Easy! // from 1 to 100. Easy!
// Just pass lambda expressions as template parameters! // Just pass lambda expressions as template parameters!
auto num = iota(1, 101).filter!(x => x % 2 == 0) auto num = iota(1, 101).filter!(x => x % 2 == 0)
.map!(y => y ^^ 2) .map!(y => y ^^ 2)