Created
November 10, 2014 13:08
-
-
Save majiang/aa2e289846a1f703d6d3 to your computer and use it in GitHub Desktop.
A function which receives the index and lazy arguments, and returns one of the arguments at the index.
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.stdio; | |
/// won't compile. | |
auto choose(T)(size_t i, lazy T[] choice...) | |
{ | |
return choice[i]; | |
} | |
/// to be generalized. | |
auto choose(T)(size_t i, lazy T c0, lazy T c1) | |
{ | |
return i ? c1 : c0; | |
} | |
auto arg(T)(T x) | |
{ | |
"evaluated %d".writefln(x); | |
return x; | |
} | |
/// expecting exactly two lines of stdout. | |
void main() | |
{ | |
"chose %d".writefln(choose(2, arg(0), arg(1), arg(2))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment