I like it too, but...
To be used effectively in such cases, you need support for
lambda. I know that it is (or was) being discussed for C++0x,
but I don't know what the current status concerning it is.
There has been some recent activity in the lambda proposals, N2329
dated 24/06/2007, which has I believe simplified useage and
implementation problems and brought them a step closer to reality.
In
the absence of lambda, if the operation is something totally
dependent on the context of the function, and not generally
applicable, trying to factor it out into a functional object
(which must be defined outside the function if it is to be used
to instantiate a template) adds complication and reduces
readability.
I'm not sure. Factoring code out into functions usually helps
legibility assuming the names are suitable. Imagine a world where
there were no functions and everything had to be declared inline.
One issue with lambda I think is that it encourages you to write
functions inline. There are 2 consequences, the first being that
'higher' functions get longer and the second that you are more likely
to end up reinventing the wheel, or have many nearly similar functions
but not quite the same scattered around to create maintenance
problems.
This is actually part of the attraction of functional style
programming for me, which is that it encourages you to think in terms
of reusable components (often with heavy use of templates). If there
is a problem in a function, it can be fixed in one place.
Its interesting in regard to the related thread "std::string and case
insensitive comparison". In there now are nearly all the components
to write a very useful functional style comparison function.
My choice would now be to use my string_pair_iterator( but try to sort
out the end condition more eficiently), then reread the part of the
discussion on to_lower and pick the best version of that, and finally
use find_if ( instead of accumulate or my custom true_for_each
function) as suggested up this thread by you I believe. The
combination looks like it should should produce an efficient
comparison function.
It took a fair amount of input to bring it all together, but it is
made from some discrete components which are reuseable and can be
recombined in many ways. aka generic programming.
Note that I'm not saying that you shouldn't give the idea some
thought. Very often, some sort of generic predicate or
functional object does make sense, and giving a name to the
operation can improve readability. But you shouldn't try to use
accumulate or for_each just to use them. The choice should be
motivated.
The motivation is that once you have solved a problem once, you don't
need to revisit it, whereas an explicit loop is a revisit each time.
Also explicit loops are inline code, which can perhaps expose low
level details to end up as a maintenance problem. I know that whenever
I write a loop I do think, I wish I could do this more elegantly..
I don't remember the exact quote or who said it, but it goes something
like.. "Apologies for writing so much. I didnt have enough time to be
brief". That is the motivation in trying to find more elegant
solutions. I would have thought its a pretty common goal...
Of course this approach doesnt help programmers paid by lines of code
written
regards
Andy Little