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

cons_tuples: (A, (B, C)) -> (A, B, C) doesn't work. Would it be possible to generalize cons_tuples to allow this? #982

Open
zekefast opened this issue Aug 6, 2024 · 4 comments

Comments

@zekefast
Copy link

zekefast commented Aug 6, 2024

Example from documentation ((A, B), C) -> (A, B, C) works pretty well!

use itertools::*;

fn main() {
    let one_two = std::iter::once((1, 2));
    let three = std::iter::once(3);

    dbg!(cons_tuples(izip!(one_two, three)).collect::<Vec<_>>());

}

but when order of tuples changes the code starts fall apart.

use itertools::*;

fn main() {
    let one_two = std::iter::once((1, 2));
    let three = std::iter::once(3);

    dbg!(cons_tuples(izip!(three, one_two)).collect::<Vec<_>>());

}

with the following error

error[E0599]: `ConsTuples<std::iter::Zip<std::iter::Once<{integer}>, std::iter::Once<({integer}, {integer})>>, ({integer}, ({integer}, {integer}))>` is not an iterator
  --> src/main.rs:7:45
   |
7  |     dbg!(cons_tuples(izip!(three, one_two)).collect::<Vec<_>>());
   |                                             ^^^^^^^ `ConsTuples<std::iter::Zip<std::iter::Once<{integer}>, std::iter::Once<({integer}, {integer})>>, ({integer}, ({integer}, {integer}))>` is not an iterator
   |
  ::: /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.13.0/src/cons_tuples_impl.rs:35:1
   |
35 | pub struct ConsTuples<I, J>
   | --------------------------- doesn't satisfy `_: Iterator`
   |
   = note: the full name for the type has been written to '/playground/target/debug/deps/playground-a1c33eced4cba996.long-type-36508219185732840.txt'
   = note: consider using `--verbose` to print the full type name to the console
   = note: the following trait bounds were not satisfied:
           `ConsTuples<std::iter::Zip<std::iter::Once<{integer}>, std::iter::Once<({integer}, {integer})>>, ({integer}, ({integer}, {integer}))>: Iterator`
           which is required by `&mut ConsTuples<std::iter::Zip<std::iter::Once<{integer}>, std::iter::Once<({integer}, {integer})>>, ({integer}, ({integer}, {integer}))>: Iterator`

For more information about this error, try `rustc --explain E0599`.

Would it possible to make it work for different structure of the tuples?

If you have some suggestions on implementations, I'm willing to make a PR.

And big thank you to all maintainers for the great library!!!

@zekefast zekefast changed the title const_tuples: (A, (B, C)) -> (A, B, C) doesn't. Would it be possible to generalize const_tuples to allow this? const_tuples: (A, (B, C)) -> (A, B, C) doesn't work. Would it be possible to generalize const_tuples to allow this? Aug 6, 2024
@scottmcm
Copy link
Contributor

scottmcm commented Aug 8, 2024

Hmm, curious. By the name cons I'd have expected it to only support (a, (b, (c, d))) order, since that want consing creates in lisp.

@zekefast
Copy link
Author

zekefast commented Aug 9, 2024

Hmm, curious. By the name cons I'd have expected it to only support (a, (b, (c, d))) order, since that want consing creates in lisp.

Yeah, you are right. Probably it would be better to create some other functions to do that.

@zekefast zekefast changed the title const_tuples: (A, (B, C)) -> (A, B, C) doesn't work. Would it be possible to generalize const_tuples to allow this? cons_tuples: (A, (B, C)) -> (A, B, C) doesn't work. Would it be possible to generalize const_tuples to allow this? Aug 14, 2024
@zekefast zekefast changed the title cons_tuples: (A, (B, C)) -> (A, B, C) doesn't work. Would it be possible to generalize const_tuples to allow this? cons_tuples: (A, (B, C)) -> (A, B, C) doesn't work. Would it be possible to generalize cons_tuples to allow this? Aug 14, 2024
@zekefast
Copy link
Author

@scottmcm What do you thing about implementing flatten for nested tuples?

@phimuemue
Copy link
Member

Just my two cents: cons_tuples seems (yet again) to be a "special-case map", and with these I increasingly think we should not offer them (or at least implement it as MapSpecialCase). My argument: Why should concatenating tuples be tied to iterators? - Tuples originate elsewhere, too.

I imagine that a dedicated fn cons_tuples((A1, A2,...), (B1, B2,...))->(A1, A2,..., B1, B2,...) would be the way to go, and if users want to use it with iterators, they can use iter.map(cons_tuples).

As for flatten: Might be a candidate for tupletools, but I think fleshing out the details for it begs some more questions, i.e. how to deal with the exponentially (?) many combinations of tuple nestings?

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

3 participants