Giter VIP home page Giter VIP logo

Comments (2)

markrogoyski avatar markrogoyski commented on July 20, 2024

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

from itertools-php.

Danon avatar Danon commented on July 20, 2024

@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.

from itertools-php.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.