September 2016
Updated: December 2018
import std.stdio, std.algorithm;
void main()
{
string inputLine;
string[] tempArray;
while((inputLine = stdin.readln()) !is null)
tempArray ~= inputLine;
sort!((a, b) => a > b)(tempArray); // descending order
foreach(line; tempArray)
write(line);
}
Classic programming
// Sort lines
import std.stdio, std.array, std.algorithm;
void main()
{
stdin
.byLineCopy
.array
.sort!((a, b) => a > b) // descending order
.each!writeln;
}
D component programming
D aims to be a useful programming language... D's job is to get s*** done quickly and effiently and make money for the businesses that use it.- D forum, July 2016
struct Boo { int x; };
void foo(struct Boo* boo)
{
if(boo != NULL)
{
boo->x = 3;
}
}
struct Boo { int x; }
void foo(Boo* boo)
{
if(boo !is null)
{
boo.x = 3;
}
}
interface Cat { void roar(); }
class Tiger : Cat
{
public:
void roar(){ privateRoar();}
private:
void privateRoar(){ writeln("Roar!"); }
}
void main()
{
Tiger tony = new Tiger();
tony.roar();
}
immutable int x = 3;
x = 5; // error
int square(int x) pure
{
return x * x;
}
int[] data = [1, 2, 3, 4, 5];
auto oddData = data.filter!(x => x%2 == 1);
module danatic.list;
import std.array : array;
struct List
{
import std.process : spawnShell;
void print()
{
import std.stdio : write;
// implement print
}
}
class List(T, U)
{
List add(T t, U u)
{
//do the add
}
}
void main()
{
auto list = new List!(int, string);
list.add(3, "foo");
}
int[5] a = [1, 2, 3, 4, 5];
int[] b = a[2..$];
b[0] = 1;
int square(int i)
{
return i * i;
}
enum x = square(3);
int y = square(5);
try { dangerousCode();}
catch(Exception e) { //process the exception }
finally { //always run this code }
void safeTransaction(Transaction foo) nothrow
{
atomicTransaction(foo);
scope(failure) rollbackTransaction(foo);
process(foo);
scope(exit) unprocess(foo);
saveToDisk(foo);
scope(success) writefln("exited with a safe transaction");
}
class Squareroot
{
long run(long x)
in { assert(x >= 0); }
out (result)
{
assert((result * result) <= x && (result+1) * (result+1) > x);
}
body
{
return cast(long)std.math.sqrt(cast(real)x);
}
unittest
{
Squareroot sqroot = new Squareroot;
assert(sqroot.run(3) == std.math.sqrt(3);
assert(sqroot.run(-2) == std.math.sqrt(-2)); //error
}
}
int a = 5;
mixin("int b = a;"); // b exists and is now 5
Regular Expressions