We have already seen an example of where we had a fair amount of duplication:
const Sums sumsWithFunctionalLoops(const vector<int>& numbers){
Sums theTotals;
vector<int> evenNumbers;
copy_if(numbers.begin(), numbers.end(), back_inserter(evenNumbers),
isEven);
theTotals.evenSum = accumulate(evenNumbers.begin(),
evenNumbers.end(), 0);
vector<int> oddNumbers;
copy_if(numbers.begin(), numbers.end(), back_inserter(oddNumbers),
isOdd);
theTotals.oddSum= accumulate(oddNumbers.begin(), oddNumbers.end(),
0);
theTotals.total = accumulate(numbers.begin(), numbers.end(), 0);
return theTotals;
}
We managed to reduce it using functions, as shown in the following code:
template<class UnaryPredicate>
const vector<int> filter(const vector<int>& input, UnaryPredicate filterFunction){
vector<int> filtered;
copy_if(input.begin(), input.end(), back_inserter(filtered),
filterFunction);
return filtered;
}
const int sum(const vector<int>& input){
return accumulate(input.begin(), input.end(), 0);
}
const Sums sumsWithFunctionalLoopsSimplified(const vector<int>& numbers){
Sums theTotals(
sum(filter(numbers, isEven)),
sum(filter(numbers, isOdd)),
sum(numbers)
);
return theTotals;
}
It's interesting to see how the functions are composed in various ways; we have sum(filter()) called twice, and sum() called once. Moreover, filter can be used with multiple predicates. Additionally, with a bit of work, we can make both filter and sum polymorphic functions:
template<class CollectionType, class UnaryPredicate>
const CollectionType filter(const CollectionType& input, UnaryPredicate filterFunction){
CollectionType filtered;
copy_if(input.begin(), input.end(), back_inserter(filtered),
filterFunction);
return filtered;
}
template<typename T, template<class> class CollectionType>
const T sum(const CollectionType<T>& input, const T& init = 0){
return accumulate(input.begin(), input.end(), init);
}
It's now easy to call filter and sum with arguments of type other than vector<int>. The implementation is not perfect, but it illustrates the point that I'm trying to make, that is, small, immutable functions can easy become polymorphic and composable. This works especially well when we can pass functions to other functions.