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

Shouldn't Multi.zip() throw exceptions for collections of different sizes? #1

Closed
danon opened this issue Jul 21, 2022 · 2 comments
Closed

Comments

@danon
Copy link

danon commented Jul 21, 2022

I supposed zipping of collections like in python is supposed to work for collections that are of the same size. If they are different, I think it would be a good idea to throw an exception in that case.

BTW, cool library, nice ReadMe.md and good tests!

@markrogoyski
Copy link
Owner

Hi @danon,

Thanks for your kind words and interest in IterTools PHP.

For zipping iterables that are different sizes, I think you can make a good argument for both throwing exceptions and iterating as much as you can. The behavior that the library has for zip is to iterate up until the point that the shortest input iterable is exhausted. Python's built-in zip function was the original inspiration for this library--I wanted something similar in PHP. So for IterTools PHP's zip function, I went with Python's behavior because it is well known and provides the most flexibility in how you can use it. Also, each use case is different. For the cases where this is not desirable, the application code could simply check the input sizes prior to zipping and throw the exception there.

FYI: Here is how Python works zipping iterables of different sizes:

>>> for x, y in zip([1, 2, 3], ['a', 'b']):
...     print(x, y)
...
1 a
2 b
>>> for x, y in zip([1, 2], ['a', 'b', 'c']):
...     print(x, y)
...
1 a
2 b

The library also provides a Multi::zipLongest function that will iterate every element of the largest iterable if one has more than another, providing null values for the iterables that run out of data. So there are multiple ways to deal with zipping inputs of different sizes.

Thanks again for your interest in IterTools PHP!
Mark

@danon
Copy link
Author

danon commented Jul 23, 2022

@markrogoyski

You could design API in sucha a way:

  • Multi::zipLongest() - assume different lengths - iterates with the higher of the iterators
  • Multi::zipShortest() - assume different lengths - iterates with the lower of the iterators
  • Multi::zip() - assumes equal iterators, throws exception otherwise

But of course, that's your library and your decision, so I'm not gonna push it, just asking.

To me, throwing exception is more natural, I rarely ever zip collections of different sizes, so I would prefer the exception. I'm leaving the decision to you.

markrogoyski pushed a commit that referenced this issue Dec 28, 2022
Method Single::filterUnique() added.
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