# C++ Refactoring Problem
Explain what the following C++14 code is doing. What benefit might be gained by this design using a custom `pow` over using `std::pow`?
```cpp
#include <iostream>
#include <numeric>
#include <array>
#include <random>
#include <algorithm>
double constexpr pow(double base, int exponent) {
return exponent == 0 ? 1 : base * pow(base, exponent - 1);
}
template<int I>
struct PowSum {
double operator()(double acc, double val) const {
return acc + pow(val, I);
}
};
std::vector<double> dataAccumulation(std::array<double, size> & data)
{
auto result = std::accumulate(std::begin(data), std::end(data), 0, LAMBDA(double acc, double val){return acc + val;});
auto result2 = std::accumulate(std::begin(data), std::end(data), 0, LAMBDA(double acc, double val){return acc + val*val;});
return {result, result2};
}
int main() {
constexpr std::size_t size = 1000000;
std::array<double, size> data;
std::random_device device;
std::mt19937 gen{device()};
std::poisson_distribution<> dist{4};
auto poisson_generator = std::bind(dist, gen);
std::generate(std::begin(data), std::end(data), poisson_generator);
std::dataAccumulation (data);
std::cout << data.at(0) << ", "
<< data.at(1) << " ... "
<< data.at(size-1) << " -> "
<< result << ", "
<< result2 << std::endl;
return 0;
}
```
Rewrite the accumulate argument using an inline lambda instead of using `PowerSum` and `pow`. If you don’t know the syntax for a lambda, pretend it is `LAMBDA(args){body}`. If you do know the lambda syntax, what might `LAMBDA` be `#define`'d as?
How might you change this code to test it? Propose a potential unit test (don’t worry about a complete example or a specific framework, etc, just outline)
Can you make a tiny (hint: one character) change to make this valid `C++11` code? Do you see any simplifications (hint: at least two characters can be removed) that could be made if this was C++17 code?
<!--
Copy of code:
```cpp
#include <iostream>
#include <numeric>
#include <array>
#include <random>
#include <algorithm>
double constexpr pow(double base, int exponent) {
return exponent == 0 ? 1 : base * pow(base, exponent - 1);
}
template<int I>
struct PowSum {
double operator()(double acc, double val) const {
return acc + pow(val, I);
}
};
int main() {
constexpr std::size_t size = 1'000;
std::array<double, size> data;
std::random_device device;
std::mt19937 gen{device()};
std::poisson_distribution<> dist{4};
auto poisson_generator = std::bind(dist, gen);
std::generate(std::begin(data), std::end(data), poisson_generator);
auto result = std::accumulate(std::begin(data), std::end(data), 0, PowSum<1>());
auto result2 = std::accumulate(std::begin(data), std::end(data), 0, PowSum<2>());
std::cout << data.at(0) << ", "
<< data.at(1) << " ... "
<< data.at(size-1) << " -> "
<< result << ", "
<< result2 << std::endl;
return 0;
}
```
-->