Since today is apparently a good day for...unlikely FizzBuzz implementations, I decided to contribute another, this one in C++:
#include <iostream>
#include <functional>
#include <vector>
#include "xrange.h"
int main() {
std::vector<std::function<void(int)> > funcs {
[](int i) { std::cout << i << "\n"; },
[](int i) { std::cout << "fizz\n"; },
[](int i) { std::cout << "buzz\n"; },
[](int i) { std::cout << "fizzbuzz\n"; }
};
auto test = [](int i){return (i % 3 == 0) + 2 * (i % 5 == 0); };
for (auto i : xrange(1, 101))
funcs[test(i)](i);
}
This uses the same xrange
I posted in a previous answer.
I'd be particularly interested in eliminating having to use test
, but a sequence like: [[
is parsed as introducing an attribute rather than using the return from a lambda as an index (and inserting white space doesn't seem to help). I could use .at()
instead, but that imposes runtime overhead I'd also rather avoid.
funcs[([]...)]
? Note that anything using a lambda in the index brackets is basically obfuscation. \$\endgroup\$funcs[0+[](int...
and when talking about ugliness you could also use the explicit operator name:funcs.operator[]([]...
\$\endgroup\$funcs[[](int i){return (i % 3 == 0) + 2 * (i % 5 == 0); }(i)]
overfuncs[(i % 3 == 0) + 2 * (i % 5 == 0)]
(which is already a mess)? \$\endgroup\$