/D 2.0 | |
//gdmd-4.6 <fitxer> | |
//Algorisme: implementació dels algorismes genèrics i els seves estructures de dades | |
//ordenar alfabèticament: | |
import std.conv, std.stdio, std.socket, std.socketstream, std.stream, std.string; | |
import std.datetime, std.exception, std.functional, std.math, std.uri; | |
/** @class Algorisme | |
** @param: U - Domini | |
** @param: V - Codomini (Rang) | |
** @field: nom - nom de l'algorisme | |
** @field: versio - versió de l'algorisme (versió inicial 1) | |
** @field: funcio - funció que s'aplica (de Domini U i Codomini V) | |
**/ | |
public class Algorisme(U,V) { | |
string nom; //!< nom de l'algorisme | |
uint versio; //!< versió de l'algorisme | |
alias V delegate (U) Funcio; //!< funció que defineix l'algorisme | |
Funcio funcio; | |
this(const string nom, const uint versio, Funcio funcio) { | |
try { | |
this.nom = nom; | |
this.versio = versio; | |
this.funcio = funcio; | |
} | |
catch (Exception exc) { | |
writeln("Error: ", exc.msg); | |
} | |
} | |
string toString() { | |
return format("%s (versió %s): %s -> %s", nom, versio, typeid(U), typeid(V)); | |
} | |
//f: T -> U, g: U -> V; g·f = g(f): T -> V. this és g perquè està a l'esquerra | |
Algorisme!(T,V) opBinary(string op, T)(Algorisme!(T,U) alg) { | |
if (op=="*") { | |
return new Algorisme!(T,V)(this.nom ~ " · " ~ alg.nom, 1, (T t) { return this.funcio(alg.funcio(t)); } ); | |
} | |
} | |
//Definició de opCall per cridar Algorisme(u) en comptes de Algorisme.funcio(u) | |
V opCall(U u) { | |
return funcio(u); | |
} | |
} | |
alias Algorisme!(int, int) AlgorismeEnters; | |
/* @class URL | |
** @field: uri - the string of the url itself | |
@method: isURL? | |
@method: constructor(string chain, checkvalidity=True) - constructs the URL with chain as uri after checking that chain is valid (if checkvalidity is True) | |
*/ | |
public class URL { | |
string uri; | |
this(const string cadena, bool checkvalidity=true) { | |
if (checkvalidity == false) { | |
this.uri = cadena; | |
} | |
else { | |
this.uri = ""; | |
} | |
} | |
} | |
void main(string [] args) | |
{ | |
enum codi = "(int a) { return 2 * a; }"; | |
auto alg = new AlgorismeEnters("Doblar", 1, mixin(codi)); | |
auto alg2 = new AlgorismeEnters("Triplicar", 1, (int a) {return 3 * a;}); | |
writeln(alg); | |
writeln(alg2); | |
writeln(alg * alg2); | |
writeln("Alg(3) = ", alg.funcio(3)); | |
writeln("Alg2(3) = ", alg2.funcio(3)); | |
writeln("Alg(Alg2(3)) = ", (alg * alg2).funcio(3)); | |
writeln("[Opcall] Alg (Alg2(3)) = ", (alg * alg2)(3)); | |
//Prova que tot va bé per funcions generals | |
auto f = new Algorisme!(int, string)("a string", 1, (int a) {return to!string(a); }); | |
auto g = new Algorisme!(string, string)("duplica", 1, (string a) {return a~a; }); | |
writeln("f: ", f); | |
writeln("g: ", g); | |
writeln(g * f); | |
writeln("g * f (3) =", (g * f).funcio(3)); | |
//URI | |
auto ur = encode("http://www.google.com/holaç"); | |
writeln(ur, typeid(ur)); | |
writeln(decode("http://www.google.com/holaç")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment