On 23 Jul, 09:26, James Kanze <james.ka...@gmail.com> wrote:
> On Jul 23, 2:33 am, kwikius <a...@servocomm.freeserve.co.uk> wrote:
>
> > On 22 Jul, 19:37, James Kanze <james.ka...@gmail.com> wrote:
> > > On Jul 21, 11:10 pm, kwikius <a...@servocomm.freeserve.co.uk> wrote:
> > > > On 20 Jul, 05:51, Skirmish <skitz_stud...@hotmail.com> wrote:
> > > > > I'd like to replace the following 5 lines of code with a std::for_each
> > > > > or std::accumulate
> > > Would it be too much to ask why? If the code clearly expresses
> > > your intent, why should you want to replace it with something
> > > else?
> > A steam engine works well enough too 
> > I guess the real answer is that functional style programmming can,
> > when you hit the sweet spot, be extremely satisfying compared to
> > loops.
>
> I like it too, but...
>
> > It is somewhat elusive in C++ however, which is not helped by
> > the requirement to use explicit iterators and lack of 'auto'. As I
> > understand these problems are being addressed in C++0x, then I expect
> > there will be more interest in, and use of functional style
> > programming in C++.
>
> 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