I believe that new features in C ++ really allow pure functional programming in this giant language, if the programmer intends to do this. I tried one of them, without any variables, and loops only based on recursion, as purely functional. I wonder if there could be some practical advantages when doing this with C ++. Well, C ++ is faster than any other functional languages, and thus, although this can be quite problematic for a programmer, it directly reduces all the benefits and security of the functional programming paradigm. Or maybe the C ++ current time compilers are too dumb to effectively optimize such code?
#include <iostream>
#include <functional>
#include <cstdint>
int main() {
std::function<const uint64_t (const uint64_t)> factorial = [&](const uint64_t n)
-> const uint64_t {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
};
{
enum { NUM_ITERATION = 20 };
std::function<void (const unsigned)> loop = [&](const unsigned n) -> void {
if (n == 0) {
return;
}
std::cout << factorial(NUM_ITERATION - n) << '\n';
loop(n - 1);
};
loop(NUM_ITERATION);
}
return 0;
}