I've decided to write a C++ FizzBuzz with the focus on some new C++11 features, ridiculous optimizations and unit-testability.
Are there improvement possibilities in those regards?
template <typename T>
static void fizzbuzz(std::ostream& out, const T& number, const std::vector<std::pair<std::string, T>>& values)
{
bool found = false;
std::for_each(values.begin(), values.end(),
[&](decltype(*values.begin()) i)
{
if (0 == number % i.second)
{
out << i.first.c_str();
found = true;
}
}
);
if (!found)
{
out << number;
}
}
template <typename T>
static void filter_fizzbuzz(const T& end, std::vector<std::pair<std::string, T>>& values)
{
// Needs to be sorted as the filtering only looks for divisible numbers up to the current one
std::sort(values.begin(), values.end());
for (auto i = values.begin(); i != values.end();)
{
// i > end will never become 0
if (i->second > end)
{
i = values.erase(i);
continue;
}
// Is the current number already handled by a divisor?
bool alreadyHandled = false;
for (auto j = values.begin(); j != i; j++)
{
if (0 == i->second % j->second)
{
alreadyHandled = true;
}
}
if (!alreadyHandled)
{
i++;
continue;
}
i = values.erase(i);
}
}
template <typename T>
void fizzbuzz(std::ostream& out, const T& start, const T& end, std::vector<std::pair<std::string, T>>& values)
{
if (start > end)
throw std::out_of_range("start > end");
if (!values.size())
return;
filter_fizzbuzz(end, values);
for (T i = start; i < end; i++)
{
fizzbuzz(out, i, values);
out << std::endl;
}
}
main()
? \$\endgroup\$AbstractGenericFizzBuzzFactorySingletonFactoryProxy
? :p \$\endgroup\$