Skip to content

Instantly share code, notes, and snippets.

@Excedrin
Created September 3, 2012 19:33
Show Gist options
  • Save Excedrin/3612698 to your computer and use it in GitHub Desktop.
Save Excedrin/3612698 to your computer and use it in GitHub Desktop.
imperative foldl and foldr
#!/usr/bin/rdmd
import std.stdio;
int sub(int x, int y) {
return x - y;
}
void main() {
int init = 0;
foreach (x; [1,2,3]) {
init = sub(init, x);
writeln("x: ", x);
}
writeln("init: ",init);
init = 0;
foreach (x; [1,2,3].reverse) {
init = sub(x, init);
writeln("x: ", x);
}
writeln("init: ",init);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment