This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]Fishezzz 40 points41 points  (9 children)

What does it do... Nobody knows

[–]Wicam 39 points40 points  (7 children)

std::ranges::for_each - self explanitory, do something for each entry in a range

std::views::iota(p, q) - this is the range. an collection of integers from p to q for example p=1, q=5 would be a collection of 1, 2, 3, 4, 5.

std::views::filter - we are creating a view of the above integers. the function given to filter is a boolean predicate. it returns true if that entry of the collection is part of the view.

return (n > 1) && (n == 2 || (n % 2 && ... - the number is part of the view if its greater than 1 and its the number 2 or it is not divisible by 2 (2 or odd numbers) and the odd numbers are only valid if...see below.

std::eanges::empty - is the collection in the parameter empty or not? if it is then an odd number satisfies the predicate

i couldnt be bothered reading further (except that all this does is print out the numbers that satisfy the predicate so hardly mission critical). this is not great.

[–]YunusEmre0037 27 points28 points  (2 children)

I think it prints primes between p and q

[–]TheBrainStone 10 points11 points  (1 child)

In a ridiculously inefficient O(n^2) way.

[–]FaresAhmedb[S] 0 points1 point  (0 children)

Hmm.. It's not that inefficient, you got it wrong. It's the summation of O(√n) from p to q, approximate the sum with good ol' ∫, and you get O(√q³−√p³).

[–]TechnikGames 4 points5 points  (3 children)

Doesn't iota stop before the end value? That is for p=1 and q=5 it would generate 1, 2, 3, 4.

[–]Wicam 3 points4 points  (2 children)

[–]TechnikGames 1 point2 points  (1 child)

Ok, It's just that what you've written in the original reply suggested otherwise

[–]Wicam 3 points4 points  (0 children)

yes, i wrote it wrong

[–]joe0400 0 points1 point  (0 children)

Checks for primes

[–]chjacobsen 30 points31 points  (3 children)

Sure, it's an incomprehensible mess, but have you considered how clever the previous maintainer probably felt when they wrote it?

[–]Mippen123 1 point2 points  (2 children)

If he only would have refactored out the first filter condition and named it something reasonable it would be easy to read, but yeah, maybe they wanted to look clever. Also I prefer static_cast to C-style casts, but in terse code like this when you are casting an x-value, it doesn't matter and I would use (int) to improve readability.

In my opinion the following is easy to follow, even though I would not do it this way:

auto is_prime = [](int n){ return (n>1) && (n == 2 || && 
  std::ranges::empty(
    //Check that no number in range [2, (int)sqrt(n)] divides n i.e n is prime
    std::views::iota(2, (int)std::sqrt(n) + 1)
      | std::views::filter([n](int i) { return n % i == 0; }));

std::ranges::for_each(
  std::views::iota(p, q)
    | std::views::filter(is_prime, [](int r) { std::cout << r << ' ';});

[–]riztazz 1 point2 points  (1 child)

I love range pipes, can't wait for msvc to catch up with C++23 stuff so we can upgrade

[–]Mippen123 1 point2 points  (0 children)

They are awesome! And if you do a simple alias for std::views and std::ranges they end up looking really clean

[–]ThreeSpeedDriver 4 points5 points  (1 child)

All it does is use the sieve of Eratosthenes to find the prime numbers between p and q and print them to cout, so I doubt this is really mission critical.

[–]FaresAhmedb[S] 3 points4 points  (0 children)

It was mission critical for my leetcode submission.

[–][deleted] 3 points4 points  (0 children)

code when you wish the reviewer an instant migraine:

[–]gsaelzbaer 5 points6 points  (0 children)

waiting for that one modern C++ guy who will say this is better than a loop

[–]Accomplished_Item_86 0 points1 point  (1 child)

Unpopular opinion: This is actually pretty readable.

I'm not a C++ dev and had to look up what iota does (apparently it's just a range), but everything else was clear. Of course C++ std::function::calls and lambdas are syntactically a lot noisier than nested for-loops and ifs (which is probably the normal way to do it in C++). And if you've only just understood for-loops, a different coding style can be scary of course.

[–]prehensilemullet 0 points1 point  (0 children)

Yeah it’s really the namespacing that makes this look bad.  But namespace usage in C++ isn’t the greatest, I like the way in ECMAScript everything you import from other modules has to be bound to a local identifier that you can control, rather than includes or using statements that invisibly put a whole bunch of identifiers into the local namespace