Menu

0-length lazy tuples

2000-10-04
2000-10-04
  • Alex Martelli

    Alex Martelli - 2000-10-04

    VERY interesting concept!

    I'm a bit perplexed about the implementation of 0-length lazy tuples -- shouldn't the tests in class LazyTuple be
    self._length >= 0
    rather than
    self._length > 0
    ...?
    A zero-length tuple seems a natural thing, lazy or otherwise, and indeed non-terminating lazy tuples are encoded with _length<0, but the tests seem to imply that a _length that is ==0 has strange properties; e.g. the tuple appears to be non-terminating, if looping over it with a for the function is still called once, etc, e.g.:
    >>> import functional
    >>> lz=functional.LazyTuple(length=0)
    >>> for i in lz:
    ...     print i
    ...    
    Traceback (innermost last):
      File "<interactive input>", line 1, in ?
      File "functional.py", line 538, in __getitem__
        val = self._itemFunc(i, self)
    TypeError: call of non-function (type None)
    >>>

    Fixing the tests to >= 0, e.g. in LazyTuple:
        def isTerminating(self):
            """
            Return 0 if this is a finite tuple, 1 if it is infinitely long.
            """
            return self._length >= 0

        def __getitem__(self, i):
            if self._length >= 0 and i >= self._length:
                raise IndexError, i

    (etc) does seem to fix this correctly, i.e.:

    >>> lz=functional.LazyTuple(length=0)
    >>> for i in lz:
    ...     print i
    ...    
    >>>

    Am I missing something...?

    Thanks!

    Alex

     
    • Bryn Keller

      Bryn Keller - 2000-10-04

      Thanks, I'm glad you like it!

      You're absolutely right, there's no reason not to support 0 length lazy tuples, it's an
      oversight I'll remedy in the next release.

      Thanks for your help!

      Bryn

       

Log in to post a comment.