In the boost library, we use accumulators like this:
acc(1); // push things into acc
cout << max( acc ) << endl; // get its result
Why can't we define its interface like this:
acc.push(1);
cout << acc.max() << endl;
So why do the accumulators from the boost library have a function-like interface? What is the advantage of it?
Here's my guess
The reason that operator
()is used for pushing instead ofpush()is becauseacc(value)reads "accumulate another value" which sounds more natural thanacc.push(value)which is "push a value to the accumulator"Besides the accumulator can receive optional features like covariate or weight and that way probably the result looks and sounds better than
acc.push(value, feature). Some examples from the documentation:And then
max(acc)is used instead ofacc.max()because it allows extensibility while still maintaining consistency. By writing a new function that receives an accumulator you'll be able to do other things to the accumulated listIf a member method was used then there'll only be a limited number of default operations like
acc.mean(),acc.max(), the remaining things must be used like a function such asproduct(acc),rolling_moment<2>(acc),kurtosis(acc),skewness(acc)... which breaks consistency