Skip to content

Instantly share code, notes, and snippets.

@PhilippeSigaud
Created May 10, 2012 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilippeSigaud/2655583 to your computer and use it in GitHub Desktop.
Save PhilippeSigaud/2655583 to your computer and use it in GitHub Desktop.
Algorithm with tuple
//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;
/**
* Helper template, to determine if a type A is an Algorisme or not.
*/
template isAlgorisme(A)
{
static if (is(A a == Algorisme!(V,U), V, U...))
enum isAlgorisme = true;
else
enum isAlgorisme = false;
}
/** @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(V,U...) {
alias U Domini; // To make it accessible outside Algorisme
alias V Codomini; // same idea
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(string nom, uint versio, Funcio funcio) {
try {
this.nom = nom;
this.versio = versio;
this.funcio = funcio;
}
catch (Exception exc) {
writeln("Error: ", exc.msg);
}
}
override 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!(V,RHS.Domini) opBinary(string op, RHS)(RHS alg)
if ( isAlgorisme!RHS
&& is(RHS.Codomini == Domini[0])
&& op == "*")
{
return new Algorisme!(V,RHS.Domini)(this.nom ~ " · " ~ alg.nom, 1, (RHS.Domini 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!(string, int)("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