This comes up often enough that I’m wondering whether it would be nice to have something standard-library-y in a zip
-ish kind of way to handle it.
I put together this. Thoughts?
This comes up often enough that I’m wondering whether it would be nice to have something standard-library-y in a zip
-ish kind of way to handle it.
I put together this. Thoughts?
2 Comments
Only works for single-pass sequences.
There’s another construction that might be more suitable for infinite sequences. The one you give traverses the cartesian plane row-by-row: if the two sequences are `[x0, x1, x2, …]` and `[y0, y1, y2, …]` it exhausts `[(x0, y0), (x0, y1), (x0, y2), …]` before starting on `[(x1, y0), (x1, y1), …]`. If the second sequence is infinite, then you’ll never get a pair with first coordinate `x1`.
An alternative construction traverses the plane in diagonals: https://en.wikipedia.org/wiki/Countable_set#/media/File:Pairing_natural.svg That means you reach any particular pair after a finite prefix, instead of having some pairs “hidden” behind an infinite prefix.
The obvious way to implement that is to keep a list of partially-evaluated `seq2` iterators, and add a new one to that list for each new diagonal: that uses increasing amounts of memory as you get further into the product-sequence, so this construction should only be used when it’s needed, but it’s a good one to know about if you’re working with infinite sequences.