5 ms·
in C++ I would write for (int i: numbers) if (i % 2 == 1) return true; return false; find_if() etc. is fine if you already have the righ
by jackewiehose 5y ago
in C++ I would write
for (int i: numbers)
if (i % 2 == 1)
return true;
return false;
find_if() etc. is fine if you already have the right predicate function
at hand but it becomes quite ridiculous with lambda expressions.
- secondcoming 5y agoThis interestingly generates shorter code as the compiler doesn't attempt any loop unrolling https://godbolt.org/z/eebE9eYP6 https://godbolt.org/z/eebE9eYP6
- kragen 5y agoI probably would too, and composability would suffer further. The concern you raise about lambda expressions is basically one of syntax rather than semantics. Syntax is important, but as an unrepentant user and occasional implementor of Lisps and Forths, I am in no position to criticize C++ :)
- Zababa 5y agoWhat I like about your example is that it's way better at communicating intent than the other C++ examples. In JS I would probably use numbers.some(number => (number % 2) === 1) I feel like the other C++ examples given are noisy.