Thursday, January 05, 2006

Functional Programming Observation Continues...

A follow-up to my previous post on functional programming in C++.
Compare

size_t total = 0;
for(size_t i = 0; i < elements_size(), ++i)
total += elements_[i].bytes();
to

size_t total = 0;
std::for_each(elements_.begin(), elements_.end(), count_bytes(total));
return total;

Neither one is "right."

What I want is to hide the details of iterating through a collection while revealing what is actually being done. In fact, I want to say:

size_t total = 0;
for (each element in elements_)
total += element.bytes();
(Python anyone?)

The "for-loop" approach fails on the "hide the iteration" criteria [although I've been using for loops for so long that for(size_t i = 0; i < elements_size(), ++i) is a single conceptual chunk for me.]

The "for-each" approach also fails to hide the iteration {although familiarity with STL makes begin() ... end() into a single conceptual chunk, also.]

The "for_each" approach also hides what's actually being done -- it depends on a descriptive name (count_bytes is not bad) to provide a hint.

The "for-loop" approach shows the actual work (which is good, but clutters it up with a leftover detail from the indexing process ("elements_[i]" rather than simply "element")

I wonder if there's some way to convince the compiler to recognize:

size_t total = 0;
while(each(element, elements_))
total += element.bytes();
for any arbitrary collection (elements_) of data type (element). This would of course require that the collection obey STL rules.