Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining reversed with zip (and possibly others) #40

Closed
ptenbrock opened this issue Aug 31, 2017 · 4 comments
Closed

Combining reversed with zip (and possibly others) #40

ptenbrock opened this issue Aug 31, 2017 · 4 comments

Comments

@ptenbrock
Copy link

I tried to use reversed in conjunction with zip in a ranged-based for loop, i.e.

for (auto&& tup : reversed(zip(container1, container2))) { ... }

Unfortunately, this results in error messages stating that there is no rbegin() function. Indeed, I didn't find that function in zip.hpp. Are there any plans to add this functionality, possibly also to the other range expressions?

@ryanhaining
Copy link
Owner

ryanhaining commented Aug 31, 2017

All of the itertools have InputIterators for the sake of it being doable. You can't reverse something with input iterators without constructing a temporary container to hold the contents, but then you lose the lazy evaluation.
Supporting reverse would mean supporting types that may have rend() and rbegin() themselves, or only extending the iterator capabilities in the case of the underlying sequence (container1, etc) having a more flexible iterator type. The work involved here is huge, I don't have any plans to implement this right now.
If your two containers are of equal length and are themselves reversible, I think you could get away with

zip(reversed(container1), reversed(container2))

@ptenbrock
Copy link
Author

Thank you for your quick answer! The code you proposed works in my situation.

I will have a look if I can find a general solution involving rend() and rbegin() but I have little experience in heavy template usage.

@ryanhaining
Copy link
Owner

ryanhaining commented Aug 31, 2017

it's pretty heavy. The SFINAEing involved will need to be pretty precise if I'm picturing this correctly. Reversing is also non-obvious for many itertools. For example, consider the case where container1 and container2 have different lengths:

std::vector<int> v = {1, 2, 3};
std::string s{"abcdef"};

If you zip this zip(v, s) you'll get

{1, a}
{2, b}
{3, c}

If you reverse it reversed(zip(v,s)), what should you get? The reversed version of the above would be

{3, c}
{2, b}
{1, a}

but that doesn't match zip(reversed(v), reversed(s)):

{3, f}
{2, e}
{1, d}

@ptenbrock
Copy link
Author

I think in case of containers of different length it would be ok if the two versions with reversed() differ, as long as the rules of how they work/differ are clear.

I realized I won't have the time to work on an implementation right now and my problem has been solved, so I will close this issue. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants