Hacker News new | ask | show | jobs
by jackewiehose 1729 days 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.
3 comments

This interestingly generates shorter code as the compiler doesn't attempt any loop unrolling

https://godbolt.org/z/eebE9eYP6

I 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++ :)
What 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.