Live data from Hacker News

Lazy sequences in idiomatic C++ using iterators

ideone.com

11–20 of 20 posts

Re: Lazy sequences in idiomatic C++ using iterators

#11
post #7
post #5

All those if statements inside loops... would be a lot cleaner (and faster!) to just use a single goto statement: goto cont; for (; ; ++z) for (; x

I totally agree--thank you for bringing this up. I'm not sure it would be faster, but it is an interesting case for goto. I saw another proposal referencing the original article which used a trio of functions in a nice way to achieve something similar without the goto (and without the ifs). My main point was to illustrate the use of custom iterators.

Consider me nerd-sniped. I typed the last post on my phone, but just had to bust out the laptop to check. Noticed a couple bugs I had: the for-loop initializers need to be set, and there needs to be an empty statement after the cont: label.

The goto version only has one unconditional branch outside of a loop, rather than three conditional branches inside loops. Anyways, the speed difference shouldn't be too big, as the branches are pretty predictable (pretty much always not taken).

Quick benchmark of the original and mine, up to the first 3000 triples:

  $ c++ -O3 pyth1.cpp  && time ./a.out | md5
  33aa33d6cad59951489757e06aeb5a15
  
  real    0m5.492s
  user    0m5.484s
  sys     0m0.007s
  $ c++ -O3 pyth2.cpp  && time ./a.out | md5
  33aa33d6cad59951489757e06aeb5a15
  
  real    0m4.248s
  user    0m4.238s
  sys     0m0.011s

Re: Lazy sequences in idiomatic C++ using iterators

#12
post #5

All those if statements inside loops... would be a lot cleaner (and faster!) to just use a single goto statement: goto cont; for (; ; ++z) for (; x

All the crazy crap they added to C/C++, how was named loops and corresponding named break/continue not one of them?

Re: Lazy sequences in idiomatic C++ using iterators

#13
This can already be done using Linq in C++, which is range-based rather than iterator based:

    auto q = LINQ(
                from(z, boost::irange(1, std::numerical_limits::max()))
                from(x, boost::irange(1, z))
                from(y, boost::irange(1, x))
                where(x*x + y*y == z*z)
                select(boost::fusion::make_vector(x, y, z))
            );
    for(auto&& x:q | linq::take(10)) std::cout 
Hopefully, once we get ranges in the C++ standard library, we can add list comprehension to the language, perhaps something like this:

    auto q = [
                for (z, std::irange(1, std::numerical_limits::max()))
                for (x, std::irange(1, z))
                for (y, std::irange(1, x))
                if (x*x + y*y == z*z)
                boost::fusion::make_vector(x, y, z)
            ];
    for(auto&& x:q | std::take(10)) std::cout 

Re: Lazy sequences in idiomatic C++ using iterators

#14
post #3
post #2

This is different. They are combining streams in the original article. (‘Start with the stream of integers from 1 to infinity ’). Your code is take 10 mutable_spaghetti_function

And as a trade off you get a much cleaner and more performant solution to the same contrived problem. This is where Haskell and functional coders tend to break down. Yes its mutable, but by violating that one rule you gain so much in terms of readability and performance.

You are right about more performant, but how is it cleaner and more readable than this?

  pyth x = [(a,b,c) | a

Re: Lazy sequences in idiomatic C++ using iterators

#15
post #12
post #5

All those if statements inside loops... would be a lot cleaner (and faster!) to just use a single goto statement: goto cont; for (; ; ++z) for (; x

All the crazy crap they added to C/C++, how was named loops and corresponding named break/continue not one of them?

In this case, the goto is into the loop, not out of it, so named break/continue would not help.

And the only point of having named break/continue would be to not use goto. I don't see the point in that, since to me, this:

    for (i = 0; i 
is as readable as this:

    loop_i:
    for (i = 0; i 

Re: Lazy sequences in idiomatic C++ using iterators

#17
post #13

This can already be done using Linq in C++, which is range-based rather than iterator based: auto q = LINQ( from(z, boost::irange(1, std::numerical_limits ::max())) from(x, boost::irange(1, z)) from(y, boost::irange(1, x)) where(x*x + y*y == z*z) select(boost::fusion::make_vector(x, y, z)) ); for(auto&& x:q | linq::take(10)) std::cout Hopefully, once we get ranges in the C++ standard library, we can add list comprehe…

[deleted]

Re: Lazy sequences in idiomatic C++ using iterators

#19
I'm learning C/C++, tried to compile (http://ideone.com/T1xVLz) your code in ubuntu, with gcc-4.8, and an error about copy_n is not declared in this scope was raised.

error: ‘copy_n’ was not declared in this scope

what is the problem? Is it something to do with gcc-4.8 and shared libs?

Thanks

Re: Lazy sequences in idiomatic C++ using iterators

#20
post #14
post #3

Earlier quoted context omitted.

And as a trade off you get a much cleaner and more performant solution to the same contrived problem. This is where Haskell and functional coders tend to break down. Yes its mutable, but by violating that one rule you gain so much in terms of readability and performance.

You are right about more performant, but how is it cleaner and more readable than this? pyth x = [(a,b,c) | a

Thats not c++ code. My claim is that if you try to stay pure using C++ you end up with very very ugly code that preforms pretty poorly.
Post reply on HN