Last active
August 29, 2015 14:11
-
-
Save mmstick/0b76673e936287ecd392 to your computer and use it in GitHub Desktop.
A collection of simple D programs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.file; | |
import std.stdio; | |
static immutable ulong buffer_size = 1024 * 1024 * 10; | |
// Print the file contents of the file obtained by openFile(). | |
void print(File input) { | |
immutable ulong file_size = input.size(); | |
if (file_size < buffer_size) { | |
ubyte[] buffer; | |
buffer.length = file_size; | |
input.rawRead(buffer); | |
stdout.rawWrite(buffer); | |
buffer.destroy(); | |
} else { | |
while (!input.eof()) input.readln.write(); | |
} | |
} | |
// Check if the file exists and if so, pass it to print(). | |
void openFile(string filename) { | |
if (filename.exists) { | |
auto file = File(filename, "r"); | |
print(file); | |
file.close(); | |
} else { | |
stdout.writeln("`" ~ filename ~ "` does not exist."); | |
} | |
} | |
void main(string[] args) { | |
args.length == 1 ? print(stdin) : openFile(args[1]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
// Takes a string array and returns it as a string. | |
string join(in string[] args) { | |
string output = args[1]; | |
foreach (argument; args[2..$]) output ~= " " ~ argument; | |
return output; | |
} | |
void stdin_loop() { while (true) readln.write; } | |
void main(string[] args) { | |
(args.length == 1) ? stdin_loop() : args.join().writeln(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.conv, std.stdio, std.string; | |
// A factorList contains the original number and a list of all | |
// prime factors that make up that number. | |
struct factorList { | |
int number; | |
int list[]; | |
// toString() returns a formatted string of the factorList. | |
string toString() { | |
string output = format("%d:", number); | |
foreach (factor; list) { | |
output ~= format(" %d", factor); | |
} | |
return output; | |
} | |
} | |
// newFactorList() takes an integer and returns a factorList of that integer. | |
factorList newFactorList(int number) { | |
factorList factors; | |
factors.number = number; | |
for (int index = 2; index <= number; index += 2) { | |
if (number % index == 0) { | |
factors.list ~= index; | |
number /= index; | |
index = 0; | |
} else if (index * index > number) { | |
factors.list ~= number; | |
break; | |
} else if (index * index == number) { | |
factors.list ~= index; | |
factors.list ~= index; | |
break; | |
} | |
if (index == 2) { index = 1; } | |
} | |
return factors; | |
} | |
// factorArgs() converts the input arguments into an integer array and then | |
// prints the prime factors of each number argument. | |
void factorArgs(string input[]) { | |
immutable int numbers[] = to!(int[])(input); | |
string buffer; | |
foreach (argument; numbers) newFactorList(argument).toString.writeln(); | |
} | |
// factorStdin() reads stdin for numbers and prints the prime factors of each. | |
void factorStdin() { | |
int number = 0; | |
stdin.readf(" %s", &number); | |
newFactorList(number).toString.writeln(); | |
} | |
void main(string args[]) { | |
args.length == 1 ? factorStdin() : factorArgs(args[1..$]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
// getSequences() returns the number of sequences to print. | |
int getSequences() | |
{ | |
int sequences; | |
write("Number of sequences: "); | |
readf("%d", &sequences); | |
return sequences; | |
} | |
// fibSequence types contain a list of fibonacci numbers. | |
struct fibSequence | |
{ | |
double sequence[] = [1]; | |
double last = 0; | |
double current = 1; | |
void appendSequence() | |
{ | |
const double temp = last; | |
last = current; | |
current += temp; | |
sequence ~= current; | |
} | |
} | |
// newFibSequence() returns a new sequence of fibonacci numbers. | |
fibSequence newFibSequence(int sequences) | |
{ | |
fibSequence fib; | |
if (sequences > 1) { | |
foreach (element; 1..sequences) fib.appendSequence(); | |
} | |
return fib; | |
} | |
void main() | |
{ | |
fibSequence fib = newFibSequence(getSequences()); | |
writeln(fib.sequence); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.file; | |
import std.stdio; | |
import std.algorithm; | |
// Searches for prints lines that contain the grep phrase. | |
void grep(File input, immutable string phrase) { | |
while (!input.eof()) { | |
immutable string line = input.readln(); | |
if (line.canFind(phrase)) { write(line); } | |
} | |
input.close(); | |
} | |
// Checks for and opens the current file and passes it to grepFile(). | |
void openFile(immutable string filename, immutable string phrase) { | |
auto file = File(filename, "r"); | |
filename.exists() ? grep(file, phrase) : writeln("File does not exist."); | |
file.close(); | |
} | |
void main(immutable string[] args) | |
{ | |
if (args.length == 1) { | |
write("Usage: grep PATTERN [FILE]...\n"); | |
} else if (args.length == 2) { | |
grep(stdin, args[1]); | |
} else { | |
foreach (index; 2..args.length) openFile(args[index], args[1]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
int gcd(int x, int y) | |
{ | |
return y == 0 ? x : gcd(y, x % y); // Ternary Operator | |
} | |
void main(string args[]) | |
{ | |
auto lcd = (int x, int y) => (x * y) / gcd(x, y); // Lambda expression | |
int a, b; | |
if (args.length < 3) { | |
write("Enter two positive numbers to find the Lowest Common Multiple: "); | |
readf("%d %d", &a, &b); | |
} else { | |
a = args[1]; | |
b = args[2]; | |
} | |
writef("The LCM of %d and %d is %d.\n", a, b, lcd(a, b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment