Skip to content

Instantly share code, notes, and snippets.

@atilaneves
Created February 26, 2016 17:22
Show Gist options
  • Save atilaneves/c4b752970f654211c3dd to your computer and use it in GitHub Desktop.
Save atilaneves/c4b752970f654211c3dd to your computer and use it in GitHub Desktop.
Compile time left fold in D
import std.meta;
auto leftFold(alias F, alias T, Ts...)() {
auto leftFoldImpl(U, V, Vs...)(U accum, V value, Vs values) {
static if(values.length == 0)
return F(accum, value);
else
return leftFoldImpl(F(accum, value), values);
}
return leftFoldImpl(T, Ts);
}
static assert(leftFold!((a, b) => a + b, 0, 1, 2, 3, 4) == 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment