views:

1439

answers:

4

What might be the best way to start programming using boost lambda libraries.

A: 

Your question is a bit vague. Normally, one doesn't use a tool just to use it, but to solve a problem. What problem are you trying to solve that you think lambdas will help?

James Curran
Perhaps they just wanted to learn a new technology or skill.
Aaron
+2  A: 

Depends. Are you already well versed in functional programming concepts? If not, I'd suggest that you start in a language that is designed for functional programming instead of a procedural language with functional features stapled on. If you aren't used to coding in a functional style (it's not harder necessarily, but it's definitely different), then you'll spend more time fighting the syntax and not enough time learning to get stuff done.

As for where to start, I cut my functional teeth on Scheme, but there are plenty of good options.

Ryan
If you are already familiar with C-style syntax, I would recommend Erlang as far as functional languages go - it's a more familiar syntax and the general structure is similar, so you spend less time confused and more time programming.
coppro
+1  A: 

If you are working with a reasonably recent compiler, you can use boost. If it's not on your machine already, install it (sudo apt-get install libboost-dev on unbuntu, get the binaries from boost.org if you are on windows). Read the doc's then look at your existing code for situations where you might use them. Do you have a lot of code duplication that could be eliminated if you parametrized a function with a small piece of code, for example?

David Nehme
+13  A: 

Remaining within the boundaries of the C++ language and libraries, I would suggest first getting used to programming using STL algorithm function templates, as one the most common use you will have for boost::lambda is to replace functor classes with inlined expressions inlined.

The library documentation itself gives you an up-front example of what it is there for:

for_each(a.begin(), a.end(), std::cout << _1 << ' ');

where std::cout << _1 << ' ' produces a function object that, when called, writes its first argument to the cout stream. This is something you could do with a custom functor class, std::ostream_iterator or an explicit loop, but boost::lambda wins in conciseness and probably clarity -- at least if you are used to the functional programming concepts.

When you (over-)use the STL, you find yourself gravitating towards boost::bind and boost::lambda. It comes in really handy for things like:

std::sort( c.begin(), c.end(), bind(&Foo::x, _1) < bind(&Foo::x, _2) );

Before you get to that point, not so much. So use STL algorithms, write your own functors and then translate them into inline expressions using boost::lambda.

From a professional standpoint, I believe the best way to get started with boost::lambda is to get usage of boost::bind understood and accepted. Use of placeholders in a boost::bind expression looks much less magical than "naked" boost::lambda placeholders and finds easier acceptance during code reviews. Going beyond basic boost::lambda use is quite likely to get you grief from your coworkers unless you are in a bleeding-edge C++ shop.

Try not to go overboard - there are times when and places where a for-loop really is the right solution.

Fruny