示例#1
0
def xinterval(iterable):
    '''
    Discover the length of the smallest interval that can contain the value of
    every items in `iterable`.

    :argument iterable: iterable object

    :return: a number

    >>> from blade.xmath import xinterval
    >>> xinterval([3, 5, 7, 3, 11])
    8
    '''
    i1, i2 = tee(sorted(iterable))
    return deque(i1, maxlen=1).pop() - next(i2)
示例#2
0
def xat(iterable, n, default=None):
    '''
    :term:`Slice` off items in `iterable` found at index `n`.

    :argument iterable: :term:`iterable`
    :argument int n: index of some items in `iterable`
    :keyword default: default returned if nothing is found at `n`
    :return: :term:`iterator` of items

    >>> from blade.xslice import xat
    >>> # default behavior
    >>> xat([5, 4, 3, 2, 1], 2)
    3
    >>> # return default value if nothing found at index
    >>> xat([5, 4, 3, 2, 1], 10, 11)
    11
    '''
    return next(islice(iterable, n, None), default)
示例#3
0
 def test_deferiter(self):
     from stuf.six import next
     from stuf.iterable import deferiter
     deferred = deferiter(iter([1, 2, 3]))
     self.assertEqual(next(deferred), 1)
示例#4
0
 def test_deferfunc(self):
     from stuf.six import next
     from stuf.iterable import deferfunc
     deferred = deferfunc(lambda: 1)
     self.assertEqual(next(deferred), 1)