C ++ boost lambda libraries

What could be the best way to start programming using powerful lambda libraries.

+4
source share
3 answers

Remaining within the boundaries of the C ++ language and libraries, I would suggest first getting used to programming using the STL algorithm function templates, as the most commonly used for boost :: lambda is to replace functor classes with built-in expressions.

The library documentation itself gives you an example of what it is intended for:

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

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

When you (over-) use STL, you are under the weight of boost :: bind and boost :: lambda. This is very convenient for things like:

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

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

From a professional point of view, I think that the best way to start with boost :: lambda is to use boost :: bind, which is clear and accepted. Using placeholders in the boost :: bind expression looks much less magical than the bare boost :: lambda placeholders and finds easier adoption during code reviews. Going beyond the basic boost :: using lambda is likely to cause you grief among your colleagues if you are not in a C ++ store with bleeding.

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

+17
source

It depends. Are you well versed in functional programming concepts? If not, I suggest starting with a language that is designed for functional programming, rather than a procedural language with functional functions stitched together. If you are not used to coding in a functional style (this is not complicated, but it is definitely different), then you will spend more time struggling with the syntax and there will not be enough time for training to get everything done.

As for where to start, I shorten the functional teeth on the Scheme, but there are many good options.

+2
source

If you are working with a fairly recent compiler, you can use boost. If it is not already on your computer, install it (sudo apt-get install libboost-dev on unbuntu, get binaries from boost.org if you are on windows). Read the doc and then look at your existing code for situations in which you can use them. Do you have a lot of code duplication that can be eliminated if, for example, you parameterized a function with a small piece of code?

+1
source

Source: https://habr.com/ru/post/1277168/


All Articles