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
Feature request: .enumerate(n)
where enumeration starts with index n
instead of 0
#815
Comments
I'm doing AoC as well and I sure do But what you can do is your own super-trait of Iterator in your utilities, something like
Then
EDIT2: The other alternative is simply |
This is a neat idea! You can even make it a little more generic: use core::ops::RangeFrom;
pub fn itemize<I, A>(iter: I, mut start: RangeFrom<A>) -> impl Iterator<Item = (A, I::Item)>
where
I: Iterator,
A: 'static,
RangeFrom<A>: Iterator<Item = A>,
{
iter.map(move |e| (start.next().unwrap(), e))
} ...allowing calls like |
That's like The downside of your function is that we can't put it in a trait because of the opaque type of the function. |
I'm going to go ahead and add this to itertools. The |
I'm not sure but I feel like a "zip" version would be less efficient than |
Just to clarify, obviously it shouldn't be named |
I looked at pub struct Enumerate<I> {
iter: I,
count: usize,
}
impl<I> Enumerate<I> {
pub(in crate::iter) fn new(iter: I) -> Enumerate<I> {
Enumerate { iter, count: 0 }
}
} So implementation there would obviously be trivial, just allow starting from count 1. .enumerate()
// The rank starts at 1, not 0.
.map(|(i, hand)| (i + 1, hand)) is just a simple addition, which should't cost much performance, but it makes it a tiny bit less readable and also probably is a tiny bit slower. I don't really see any downsides to adding this to itertools (or stdlib, but that probably wouldn't happen, right?), since implementation should be fairly straightforward and maintainable, right? |
Do you think I should make this feature request to the official rust std lib instead? Like detailed in my last message, I think it would be very easy to implement this in the std lib, and it would have zero overhead (no extra |
Well they added Coming back to this, I see that |
I am currently solving Advent of Code day 07 part 1, and wrote this code:
I needed to give each iterator element a
rank
, starting at1
. Therefore, the only hacky solution I came up with was to.enumerate()
as normal, and then increment each indexi
to get the rank. I think the code would a little cleaner/idiomatic if I could write:Would it be possible to implement something like that in itertools?
Thanks for the awesome crate!
The text was updated successfully, but these errors were encountered: