We have seen, in past chapters, how to structure code in a functional way, by taking advantage of small transformations on data structures. Let's take a simple example—compute the sum of all even numbers from a list. The structured programming approach would be to write a loop that goes over the whole structure and adds all elements that are even:
int sumOfEvenNumbersStructured(const list<int>& numbers){
int sum = 0;
for(auto number : numbers){
if(number % 2 == 0) sum += number;
}
return sum;
};
The test for this function runs correctly on a simple example:
TEST_CASE("Run events and get the user store"){
list<int> numbers{1, 2, 5, 6, 10, 12, 17, 25};
CHECK_EQ(30, sumOfEvenNumbersStructured(numbers));
}
Of course, this method mutates data and we have seen that it's not always a good idea. It also...