• Create Account

### #ActualStroppy Katamari

Posted 06 November 2012 - 01:14 PM

Yes you can. Don't. You can use them frequently, but if the expression is more than a few (1 to 7, say) lines then from a readability standpoint you would benefit from writing a separate function and using std::bind if currying arguments is required. Short lambda expressions increase code readability, longer ones decrease it.

Long lambdas do not decrease code readability. The difference between this:
for (auto it=begin(container); it!=end(container); ++it)
{
... 20 lines ...
}
and this:
for_each(begin(container),end(container),[](element &x)
{
... 20 lines ...
});
is just a matter of taste. I tend to like the for_each version when I can't use range-for.
You can use lambdas of whatever length when the functionality within is a one-off and not needed anywhere else in the code.
If the code is needed several times but only locally, you can still use a lambda and avoid polluting the outer scope with useless stuff:
void someFunc()
{
auto snarf = [](foo &amp;amp;f) { /* snarfing a foo */ };
for_each(begin(manyFoos), end(manyFoos), snarf);
snarf(*begin(manyFoos)); // first foo needs to be snarfed twice
snarf(anotherFoo);
}

### #4Stroppy Katamari

Posted 06 November 2012 - 01:13 PM

Yes you can. Don't. You can use them frequently, but if the expression is more than a few (1 to 7, say) lines then from a readability standpoint you would benefit from writing a separate function and using std::bind if currying arguments is required. Short lambda expressions increase code readability, longer ones decrease it.

Long lambdas do not decrease code readability. The difference between this:
for (auto it=begin(container); it!=end(container); ++it)
{
... 20 lines ...
}
and this:
for_each(begin(container),end(container),[](element &amp;x)
{
... 20 lines ...
});
is just a matter of taste. I tend to like the for_each version when I can't use range-for.
You can use lambdas of whatever length when the functionality within is a one-off and not needed anywhere else in the code.
If the code is needed several times but only locally, you can still use a lambda and avoid polluting the outer scope with useless stuff:
void someFunc()
{
auto snarf = [](foo &amp;f) { /* snarfing a foo */ };
for_each(begin(manyFoos), end(manyFoos), snarf);
snarf(*begin(manyFoos)); // first foo needs to be snarfed twice
snarf(anotherFoo);
}

### #3Stroppy Katamari

Posted 06 November 2012 - 01:13 PM

Yes you can. Don't. You can use them frequently, but if the expression is more than a few (1 to 7, say) lines then from a readability standpoint you would benefit from writing a separate function and using std::bind if currying arguments is required. Short lambda expressions increase code readability, longer ones decrease it.

Long lambdas do not decrease code readability. The difference between this:
for (auto it=begin(container); it!=end(container); ++it)
{
... 20 lines ...
}
and this:
for_each(begin(container),end(container),[](element &amp;x)
{
... 20 lines ...
});
is just a matter of taste. I tend to like the for_each version when I can't use range-for.
You can use lambdas of whatever length when the functionality within is a one-off and not needed anywhere else in the code.
If the code is needed several times but only locally, you can still use a lambda and avoid polluting the outer scope with useless stuff:
void someFunc()
{
auto snarf = [](foo &amp;f) { /* snarfing a foo */ };
for_each(begin(manyFoos), end(manyFoos), snarf);
snarf(*begin(manyFoos)); // first foo needs to be snarfed twice
snarf(anotherFoo);
}

### #2Stroppy Katamari

Posted 06 November 2012 - 01:13 PM

Yes you can. Don't. You can use them frequently, but if the expression is more than a few (1 to 7, say) lines then from a readability standpoint you would benefit from writing a separate function and using std::bind if currying arguments is required. Short lambda expressions increase code readability, longer ones decrease it.

Long lambdas do not decrease code readability. The difference between this:
for (auto it=begin(container); it!=end(container); ++it)
{
... 20 lines ...
}
and this:
for_each(begin(container),end(container),[](element &amp;x)
{
... 20 lines ...
});
is just a matter of taste. I tend to like the for_each version when I can't use range-for.
You can use lambdas of whatever length when the functionality within is a one-off and not needed anywhere else in the code.
If the code is needed several times but only locally, you can still use a lambda and avoid polluting the outer scope with useless stuff:
void someFunc()
{
auto snarf = [](foo &amp;f) { /* snarfing a foo */ };
for_each(begin(manyFoos), end(manyFoos), snarf);
snarf(*begin(manyFoos)); // first foo needs to be snarfed twice
snarf(anotherFoo);
}

### #1Stroppy Katamari

Posted 06 November 2012 - 01:10 PM

Yes you can. Don't. You can use them frequently, but if the expression is more than a few (1 to 7, say) lines then from a readability standpoint you would benefit from writing a separate function and using std::bind if currying arguments is required. Short lambda expressions increase code readability, longer ones decrease it.

Long lambdas do not decrease code readability. The difference between this:
for (auto it=begin(container); it!=end(container); ++it)
{
... 20 lines ...
}
and this:
for_each(begin(container),end(container),[](element &x)
{
... 20 lines ...
});
is just a matter of taste. I tend to like the for_each version when I can't use range-for.
You can use lambdas of whatever length when the functionality within is a one-off and not needed anywhere else in the code.
Occasionally it also makes sense to reuse a lambda within a section of code:
void someFunc()
{
auto snarf = [](foo &f) { /* snarfing a foo */ };
for_each(begin(manyFoos), end(manyFoos), snarf);
snarf(*begin(manyFoos)); // first foo needs to be snarfed twice
snarf(anotherFoo);
}

PARTNERS