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

reverse iteration for eachsplit #43740

Open
stevengj opened this issue Jan 10, 2022 · 3 comments
Open

reverse iteration for eachsplit #43740

stevengj opened this issue Jan 10, 2022 · 3 comments
Labels
feature Indicates new feature / enhancement requests iteration Involves iteration or the iteration protocol strings "Strings!"

Comments

@stevengj
Copy link
Member

stevengj commented Jan 10, 2022

As commented in #39245 (comment), it would be nice to add iterate methods for Iterators.Reverse{SplitIterator} — in general, for any iterator where it is feasible to do so, we'd like to support reverse iteration, and this must be implemented in Base to avoid type piracy.

Currently, e.g. collect(Iterators.reverse(eachsplit("a.b.c", '.', limit=2))) gives a MethodError because it tries to fall back on the generic method that requires indexing.

(Would also be helpful in #43557, since it would give us an easy rsplit method that outputs a type-stable tuple.) This is distinct from an eachrsplit iterator.

Note that the case with a limit argument seems a bit tricky to implement, since it seems like you would first need to count all the delimiters in the string, an O(n) operation, in order to figure out how many delimiters to skip at the end.

@stevengj stevengj added strings "Strings!" iteration Involves iteration or the iteration protocol labels Jan 10, 2022
@vtjnash vtjnash added the feature Indicates new feature / enhancement requests label Jan 12, 2022
@jakobnissen
Copy link
Contributor

I would like to give a go implementing eachrsplit. However, I think the only way to do this in a non-allocating way is to return the elements as found from right to left, in the opposite order of rsplit. I simply can't think of any reasonable way to implement eachrsplit that iterates left-to-right, without making it strictly worse than rsplit.

CC'ing @stevengj @vtjnash - before I put in the work, would such a right-to-left iterator be a good addition to Base? See also #45385 and #46745

@stevengj
Copy link
Member Author

stevengj commented Oct 9, 2023

Note that an eachrsplit iterator is distinct from an Iterators.Reverse{SplitIterator} iterator:

  • Iterators.Reverse{SplitIterator} should be functionally equivalent to iterating over reverse!(collect(eachsplit(...)) … as I commented, however, it couldn't efficiently support a limit argument.
  • an eachrsplit can simply search from right-to-left, and have a limit argument; it need not be equivalent to reversing the output of eachsplit.

@jakobnissen
Copy link
Contributor

You're right, they behave differently when limit > 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests iteration Involves iteration or the iteration protocol strings "Strings!"
Projects
None yet
Development

No branches or pull requests

3 participants