rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.69k stars 303 forks source link

zip together an unknown number of iterators #828

Closed wangjiawen2013 closed 7 months ago

wangjiawen2013 commented 8 months ago

Hi, who can helps ?

https://stackoverflow.com/questions/55291961/how-can-i-zip-together-an-unknown-at-compile-time-number-of-iterators

https://stackoverflow.com/questions/29669287/how-can-i-zip-more-than-two-iterators

Philippe-Cholet commented 8 months ago

But there is no iterator that zip an unknown number of iterators. The example suggested seems decent to me

struct Multizip<T>(Vec<T>);

impl<T> Iterator for Multizip<T>
where
    T: Iterator,
{
    type Item = Vec<T::Item>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.iter_mut().map(Iterator::next).collect()
    }
}

It could at least have a size hint. And it imposes a same iterator type which does not seem very helpful. Each can be boxed vec![Box::new(0..2), Box::new(2..=5), ...] though.

Would you elaborate on what you need?

wangjiawen2013 commented 8 months ago

I want to zip columns of Polars dataframe, but sometimes there are a lot number of columns, I must put all the columns (which have been converted to vectors) using izip! manually. It's very inconvinenced.

Philippe-Cholet commented 8 months ago

That sure seems painful.

I'm not familiar with Polars but "zip columns" is not just rows? in which case there sure is a method for it. Unless it's hidden behind the crate feature "rows" that you might need to add first (based on this).