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

Infinite sequence of all combinations #293

Open
svenstaro opened this issue Jun 11, 2018 · 2 comments
Open

Infinite sequence of all combinations #293

svenstaro opened this issue Jun 11, 2018 · 2 comments

Comments

@svenstaro
Copy link

Given a list of inputs ["A", "B", "C"] I want an iterator that gives me an infinite combination output so that we get ["A", "B", "C", "AA", "AB", ..., "CCCC", .... I don't believe this is currently easily possible using itertools.

@svenstaro
Copy link
Author

Perhaps this is not "combinations" mathematically speaking but rather a product.

@tobz1000
Copy link
Contributor

This can probably be done with some adaption of MultiProduct. To add a bit more flexibility, it could take a meta-iterator, incremenally yielding items from the first iterator, then the product of the first two, etc. until termination (or not, if the meta-iterator is infinite).

e.g.

for x in (0..4).map(|_| 0..=1).incremental_product() {
    // [0] -> [1] -> [0, 0] -> [0, 1] ... [1, 1, 0, 1] -> [1, 1, 1, 0] -> [1, 1, 1, 1]
}

Then your use case could be achieved with

repeat(&["A", "B", "C"]).incremental_product()

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

2 participants