I'm sure the Boost Lambda Library for C++ has been around for a while, but Tom just showed it to me.
Finally, the following for_each call outputs the sorted content of vp separated by line breaks:
for_each(vp.begin(), vp.end(), cout << *_1 << "\n");
But wait, it gets much better.
Note that a normal (non-lambda) expression as subexpression of a lambda expression is evaluated immediately. This may cause surprises. For instance, if the previous example is rewritten as
for_each(vp.begin(), vp.end(), cout << "\n" << *_1);the subexpression cout << "\n" is evaluated immediately and the effect is to output a single line break, followed by the elements of vp.
I'd love to see the compiler messages you get when you make a typo.
(An old post from Lambda the Ultimate has more information.)
Posted by jjwiseman at May 26, 2004 11:10 AMJohn,
Minor nit: the syntax doesn't use a "*" before the placeholder, so:
for_each(vp.begin(), vp.end(), cout
Posted by: Paul Snively on May 28, 2004 03:43 PMIf you think boost::lambda is cool, BTW, you should also check out <http://spirit.sourceforge.net/distrib/spirit_1_7_0/libs/spirit/phoenix> and <http://www.cc.gatech.edu/~yannis/fc++>. Functional programming has hit the C++ world big-time.
Posted by: Paul Snively on May 28, 2004 03:53 PMLet's try that again: http://spirit.sourceforge.net/distrib/spirit_1_7_0/libs/spirit/phoenix/index.html and http://www.cc.gatech.edu/~yannis/fc++
Posted by: Paul Snively on May 28, 2004 03:57 PMWow, phoenix makes C++ like, not TOTALLY suck!
Posted by: David Mercer on May 28, 2004 09:45 PM