Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active January 4, 2016 15:05
Show Gist options
  • Save alphaKAI/caea803d7011f14ca98a to your computer and use it in GitHub Desktop.
Save alphaKAI/caea803d7011f14ca98a to your computer and use it in GitHub Desktop.
D言語でTemplate Meta Programmingを駆使して書いた、任意の深さのnested forを生成するテンプレート
import std.stdio;
template FOR(alias func, alias cond) {
import std.algorithm,
std.traits,
std.range,
std.conv;
void FOR() {
immutable lambdaStr = (lamArgs =>
(temp) {
foreach(e; lamArgs) {
temp ~= "for(ulong " ~ e ~ "; " ~ "cond(" ~ e ~ "); " ~ e ~ "++)";
}
return temp ~ "func(" ~ lamArgs.join(", ") ~ ");";
}("")
)(arity!func.iota.map!(i => "arg" ~ i.to!string));
mixin(lambdaStr);
}
}
void main() {
FOR!((ulong i1, ulong i2) => writeln(i1 * i2), i => i < 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment