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

Feature Request: zip_pad_with_default #537

Closed
abrassel opened this issue Apr 21, 2021 · 5 comments
Closed

Feature Request: zip_pad_with_default #537

abrassel opened this issue Apr 21, 2021 · 5 comments

Comments

@abrassel
Copy link
Contributor

When trying to zip over two iterators of unequal lengths, zip_longest is very useful for the case in which one doesn't exist.
When trying to do zip longest, but the underlying items implement Default, it could be useful to have a version of zip_longest that pads the shorter iterator with Item::default().

@scottmcm
Copy link
Contributor

I don't think a new iterator adapter is the best choice here, since that's a relatively heavy and non-orthogonal addition.

The item type for zip_longest is EitherOrBoth, though, so maybe add some methods on there to make things easier?

As a sketch,

impl<T:Default> impl EitherOrBoth<T> {
    fn or_default(self) -> (T, T) { ... }
}

Then .zip_pad_with_default() could be done as just .zip_longest().map(|x| x.or_default()).

(Dunno what the best name for the method would be. I'm inspired here by https://docs.rs/itertools/0.9.0/itertools/enum.MinMaxResult.html#method.into_option)

@abrassel
Copy link
Contributor Author

That looks great to me! I've not contributed to an open source project before - at this point do I submit a pull request?

@phimuemue
Copy link
Member

That looks great to me! I've not contributed to an open source project before - at this point do I submit a pull request?

I think you could give it a try.

@jswrenn
Copy link
Member

jswrenn commented Apr 28, 2021

Please do! I'd be happy to merge this.

@abrassel
Copy link
Contributor Author

@jswrenn Done!

bors bot added a commit that referenced this issue May 2, 2021
538: Added the `or_default`  method to `EitherOrBoth` r=phimuemue a=abrassel

This commit is motivated by the context explained in issue #537 

Allows for doing things like padding out an iterator with a default value while the other one is consumed.

Co-authored-by: Alex Brassel <awildbrasselsprouts@gmail.com>
phimuemue pushed a commit to phimuemue/rust-itertools that referenced this issue May 2, 2021
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

4 participants