Code readability with c++11 lambdas - c++

I REALLY love lambdas and having the ability to use them in C++ is a pleasure. But, as I'm used to Haskell, where lambdas fit really well into the syntax, I'm struggling with how to use them in C++ without writing unreadable cluttered long code lines.
So, as an example, suppose I'd write this:
vector<double> foo(10,0.2);
for_each(foo.begin(), foo.end(), [](double x){ std::cout << x << " ";})
this is not so difficult to read, the lambda expression is pretty small. But if I have a two or three line long function inside that for_each, this could become a problem for my
code-reading-skills:
vector<double> foo(10,0.2);
randomNumberGenerator bar;
for_each(foo.begin(), foo.end(), [](double x){ std::cout << "hello!"; x+=bar()/(1+bar()); std::cout << x << " ";})
//sorry, I couldn't think of a less stupid example...
This line is starting to get annoyingly long and difficult to read for my taste...
What is your preferred code conventions for this case? Should I write:
for_each(foo.begin(), foo.end(),
[] (double x) {
std::cout << "hello!"
x += bar()/(1+bar());
std::cout << x << " ";
});
or something like it? I still think this syntax feels a bit unnatural and difficult to read... :(

I usually go for
for_each(foo.begin(), foo.end(), [](double x) {
std::cout << "hello!"
x += bar()/(1+bar());
std::cout << x << " ";
});
I've written some several hundred line lambdas.

If you prefer, you can name your lambda separately with auto:
auto const baz = [](double x)
{
std::cout << "hello!"
x += bar()/(1+bar());
std::cout << x << " ";
};
std::for_each(foo.begin(), foo.end(), baz);

Hmm...
for_each(foo.begin(), foo.end(),
[] (double x)
{
std::cout << "hello!"
x += bar()/(1+bar());
std::cout << x << " ";
});
for (auto x : foo)
{
std::cout << "hello!";
x += bar()/(1+bar());
std::cout << x << " ";
}

I like to look at lambdas as just another function declaration, and thus, follow the same conventions that I use for other functions, within reason:
// when lambdas are present, I break the enveloping method params
for_each(
foo.begin(),
foo.end(),
[] (double x)
// I also like to split the brackets, just like with any function
{
std::cout << "hello!"
x += bar()/(1+bar());
std::cout << x << " ";
}); // the closing parenthesis is left with the closing bracket

I'd say if the code for the lambda is more than one or perhaps two statements, it should be a separate named function.

Post mine
std::vector<int> a;
std::find_if(a.begin()
, a.end()
, [&](int i)
{
return i == 0;
});

Related

What is wrong with passing a function that returns a vector by value as argument of ublas::element_prod()?

I cannot seem to understand why passing directly a function as argument to ublas::element_prod() produces a wrong result.
If I run the following code:
#include <cmath>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
namespace ublas = boost::numeric::ublas;
ublas::vector<double>
vector_ln(ublas::vector<double> x) {
for(auto it = x.begin(); it != x.end(); ++it) {
(*it) = log(*it);
}
return x;
}
int main(void) {
ublas::vector<double> x(2, 2.0);
ublas::vector<double> y(2, 10.0);
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
auto tmp = vector_ln(y);
auto ret1 = ublas::element_prod(
x,
tmp);
std::cout << ret1 << std::endl;
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
auto ret2 = ublas::element_prod(
x,
vector_ln(y));
std::cout << ret2 << std::endl;
}
I get the following output:
x = [2](2,2)
y = [2](10,10)
[2](4.60517,4.60517)
x = [2](2,2)
y = [2](10,10)
[2](0,4.60517)
Can anybody enlighten me on why the second coding style produces a wrong result, with no compile error?
The problem is that ublas uses Expression templates, where the result of a number of operations is a temporary which simply keeps a pointer/reference to its inputs, and is evaluated when assigning to a variable. This is done to reduce unnecessary computations and copies. See https://en.wikipedia.org/wiki/Expression_templates
However, with the introduction of C++11, it creates a dangerous interaction with the use of auto, as this saves a copy of the expression template, and not the result. This expression template has dangling references to the temporary returned by vector_ln(y), and causes the problem you are seeing.
Since the primary problem is the interaction with auto, the solution is to save it to the correct ublas matrix type as the result of element_prod(). It only worked in the first case because none of the stored references were temporaries.

Read and write different types in vector

How can I pack in a vector several different primitive data types as well as strings to examine the contents then?
(In Java, this went via Object [] objects = {1.0, "Hello", - 42, 'b'})
The task is as follows:
Given the following array: [3.0,42, "Monkey", 7.2, b]
This array is to be passed to a method that outputs the contents of the array on the console. If it is a string, each letter of the string should be added as an ASCII value in the same variable, and finally returned as an int on the console. With char exactly the same.
I know from today as I create a vector so with std::vector<double> numbers = {1.0,2.0}; How to write functions and how to access the indexes numbers[i]; as well as the length of the vector numbers.size().
How can I solve this problem now? Since I have unfortunately found no simple <- solution for the multiple types in a vector.
Thanks in advance :)
C++ does not do type erasure the same way that Java does. To create a heterogeneous container (which is the technical term for what you're trying to do) you'll need to make extensive use of std::any or std::variant, which are new classes introduced with C++17.
std::vector<std::any> values{1.0, "Hello", -42, 'b'};
for(auto & any : values) {
int * i;
if(val = std::any_cast<int>(&any)) std::cout << "int: " << *i << std::endl;
const char ** s;
if(s = std::any_cast<const char *>(&any)) std::cout << "string-literal: " << *s << std::endl;
double * d;
if(d = std::any_cast<double>(&any)) std::cout << "double: " << *d << std::endl;
char * c;
if(c = std::any_cast<char>(&any)) std::cout << "char: " << *c << std::endl;
}
Note how messy that code is. Not least of which because many people would desire "hello" to be stored as a std::string object, but this can't be done unless the user expressly designates it as such:
std::vector<std::any> values{1.0, std::string{"Hello"}, -42, 'b'};
At any rate, my personal opinion is that the use of std::variant would be a much better fit, as you can make it much clearer how the container is meant to be used, and you can avoid the dynamic allocations associated with std::any:
typedef std::variant<std::string, char, double, int> my_variant;
struct visitor {
void operator()(std::string const& v) const {
std::cout << "std::string: " << v<< std::endl;
}
void operator()(double const& v) const {
std::cout << "double: " << v << std::endl;
}
void operator()(int const& v) const {
std::cout << "int: " << v << std::endl;
}
void operator()(char const& v) const {
std::cout << "char: " << v << std::endl;
}
};
int main() {
std::vector<my_variant> values{1.0, "Hello", -42, 'b'};
for(my_variant & variant : values) {
std::visit(visitor{}, variant);
}
return 0;
}
We can even make the variant version a lot simpler with auto lambdas if we don't need to know the type:
typedef std::variant<std::string, char, double, int> my_variant;
int main() {
std::vector<my_variant> values{1.0, "Hello", -42, 'b'};
for(my_variant & variant : values) {
std::visit(
[](auto const& val) {std::cout << "Some unknown type: " << val << std::endl;},
variant
);
}
return 0;
}
I haven't run this through my compiler, but this should give a pretty good sense of how to accomplish this kind of task in C++.
If you don't have access to C++17, you can use boost.any and boost.variant, which I'm reasonably sure are both header-only libraries, and thus easy to import into your project.

Call C++ recursive lambda in the same line where it is declared

This is mostly a one liner style type of question, I would normally write this code in multiple lines anyway for readability reasons.
So my question is can I call the recursive lambda in the same statement where it is defined?
So instead of this:
int n=3;
function<void(int)> f {[n,&f](int i){if (i>1) { cout << "func(a, "; f(i-1); cout << ")";} else cout << "a";}};
f(n);
call the function with n in the same line where f is defined.
In one statement which declares several variables ;-)
Mostly not what you want:
std::function<void(int)>
f {[&f](int i){
if (i>1) {
std::cout << "func(a, "; f(i-1); std::cout << ")";}
else
std::cout << "a";
}},
dummy((f(3), nullptr));
Demo
Let me offer a glimpse into the functional programming world, where people usually use combinators to deal with recursive lambdas. There was a proposal (P0200r0) last year to add a simple Y-combinator to the standard library.
Leaving aside the question whether it is a good idea to do this, this would allow you to write and invoke a recursive lambda like this:
y_combinator([](auto self, int i){
if (i>1) {
std::cout << "func(a, ";
self(i-1);
std::cout << ")";
} else {
std::cout << "a";
}
})(6);
The basic idea here is that the y-combinator is a higher order function that wraps a lambda which is passed 'itself' as a first argument. The combinator takes care of wrapping the self argument away for all invocations of the lambda.
You can try it in coliru.
As a matter of fact, you can. Here is a complete example which compiles and runs fine with g++ -std=c++11:
#include <iostream>
#include <functional>
int main() {
std::function<int(int)> f = ([&f](int i){ return i?f(i-1)*i:1; }), trash = (std::cout << f(3) << std::endl, f);
}
However, I don't think it's a good idea to actually use this: The construct , trash = (..., f) would be in order for code golf or obfuscated programming contests, but would not meet my standards for production code.
What you are essentially asking for is to create a temporary instance of std::function and then invoke operator() on that object in the same statement. Both of these fail to compile for me with the same error when I try that:
function<void(int)> f{[&f](int i){ if (i > 1) { cout << "func(a, "; f(i-1); cout << ")"; } else cout << "a"; }}(n);
function<void(int)> f([&f](int i){ if (i > 1) { cout << "func(a, "; f(i-1); cout << ")"; } else cout << "a"; })(n);
error: expected ‘,’ or ‘;’ before ‘(’ token
Pointing at the ( of (n).
See #Jarod42's answer for a viable workaround, if you don't mind the extra variable initialization.
Alternatively, this would work, though it does have to use separate variable declaration and assignment:
function<void(int)> f; f = [&f](int i){ if (i > 1) { cout << "func(a, "; f(i-1); cout << ")"; } else cout << "a"; }, f(n);
Demo
Not sure if you consider it valid since it doesn't use lambda functions, but it is still single line and leaves no temporal variables behind ;)
struct {
struct R {
R(int i) {
if (i>1) { cout << "func(a, "; R(i-1); cout << ")"; }
else cout << "a";
}
} r;
} f{n};

How do I stream into a string without creating a named stringstream?

I often end up writing code like this:
SomeStreamableType x;
std::stringstream ss;
ss << "Value is: " << x;
log(ss.str());
The extra line needed to generate the stringstream feels superfulous. I can do this, but it's equally cumbersom:
SomeStreamableType x;
const std::string str = "Value is: " + boost::lexical_cast<std::string>(x);
log(str);
I want to be able to do this:
SomeStreamableType x;
log(std::stringstream() << "Value is: " << x);
Have others encountered this issue and come up with a workaround? I don't want to create any helper functions or classes.
Your code will work without modifications, as long as log accepts an ostream& reference:
void log(ostream& o) {
stringstream* s = dynamic_cast<stringstream*>(&o);
if (s) {
cout << s->str() << endl;
}
}
int main() {
int x = 5, y = 6;
log(stringstream() << "x=" << x << ", y=" << y);
return 0;
}
Demo.
To solve this problem I have often simply done something like this:
#define LOG(m) do{std::ostringstream oss;oss<<m;std::cout<<oss.str()<<'\n';}while(0)
// ...
LOG("some text: " << value1 << ' ' << value2); // no need for '\n'
Now I tend to use a more sophisticated class based solution that has an even nicer interface and doesn't use a horrible macro.

How to use lambda function to print a pair<>?

I want to print a pair, e.g.
std::cout << make_pair(std::string,int) << endl;
But it doesn't compile because operator<<(ostream &, std::pair<std::string,int>) is not defined.
But because we now have c++11 with lambda functions, I can use lambda functions with a for_each expression to work on containers.
For the above case how could I supply an "in-place method" which can be used by ostream to print the pair?
Pairs (and other tuples) aren't really like containers, because their elements have heterogeneous types. They can't be iterated over in the normal way. So a lambda isn't really applicable here.
If you want, just define an output_pair template function which takes an ostream and a pair, and outputs the two elements of the pair. Or if you wanted to keep the extraction style, you could have output_pair return an output_pair_struct which does nothing but hold a copy of the tuple, and define an operator<< on the output_pair_struct which did the actual work, so that you could have std::cout << output_pair(mypair) << endl;.
For the above case how could I supply an "in-place method" which can be used by ostream to print the pair?
auto print = [&](const std::pair<std::string,int>& p) {
std::cout << p.first << ", " << p.second << "\n";
};
std::map<std::string,int> sequence = { /* ... */ };
for_each(sequence.begin(), sequence.end(), print);
for(const auto& p: sequence)
print(p);
or:
for_each(sequence.begin(), sequence.end(),
[&](const std::pair<std::string,int>& p) {
std::cout << p.first << ", " << p.second << "\n";
});
Either way, this is too complicated. You should write it like this:
for(const auto& p: sequence)
std::cout << p.first << ", " << p.second << "\n";