Created
February 26, 2016 17:22
-
-
Save atilaneves/c4b752970f654211c3dd to your computer and use it in GitHub Desktop.
Compile time left fold in D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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