Live data from Hacker News

Lazy sequences in idiomatic C++ using iterators

ideone.com

1–10 of 20 posts

Re: Lazy sequences in idiomatic C++ using iterators

#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.

Re: Lazy sequences in idiomatic C++ using iterators

#4
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.

That's one thing I've noticed with my C++ code lately is that I'm writing const absolutely everywhere. Having mutability where you need it is awesome but it is interesting to see languages like Rust where immutability is the default.

Re: Lazy sequences in idiomatic C++ using iterators

#6
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

Indeed. This is part of why I included "idiomatic" in the title here. Someone smarter than me already wrote a bit prettier iterator increment function, and using that is easy once you have the interface decided. And in C++, iterators abound, and can be used as infinite sequence generators with no difference to how they are normally used and understood.

The code may be spaghetti, but I mostly just lifted it from the referenced article and solved the problems the original author brought up, namely the separation of concerns. The C++ solution to this problem will never be as beautiful as the Haskell one, but I wanted to show it doesn't have to be alienating for people who program C++ daily.

C++ iterators are powerful, and can be used for a lot more than stepping through a container. We write too few of them.

Re: Lazy sequences in idiomatic C++ using iterators

#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.

Re: Lazy sequences in idiomatic C++ using iterators

#8
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'm always looking for a legitimate excuse to use a goto in my C++ code because I'm perverse. It's very rare to find one but this looks like it might be okay.

Re: Lazy sequences in idiomatic C++ using iterators

#9
post #4
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.

That's one thing I've noticed with my C++ code lately is that I'm writing const absolutely everywhere. Having mutability where you need it is awesome but it is interesting to see languages like Rust where immutability is the default.

I have done a bit of this too. The main downside in C++ is that you end up writing a few more ternary operators than you otherwise may have. But it is nice to be able to look at the first assignment of a variable and know it is the only one.

Re: Lazy sequences in idiomatic C++ using iterators

#10
post #9
post #4

Earlier quoted context omitted.

That's one thing I've noticed with my C++ code lately is that I'm writing const absolutely everywhere. Having mutability where you need it is awesome but it is interesting to see languages like Rust where immutability is the default.

I have done a bit of this too. The main downside in C++ is that you end up writing a few more ternary operators than you otherwise may have. But it is nice to be able to look at the first assignment of a variable and know it is the only one.

Yeah it kind of feels dirty having all these mutable variables around after doing any functional programming at all.

I've found using const a lot also goes hand-in-hand with raii. If a member of a class is const it must be initialized in the constructor, and it just seems to make me code a bit more "hygienically".

Post reply on HN