iterables.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:ww 作者: Tygs 项目源码 文件源码
def lasts(iterable, items=1, default=None):
    # type: (Iterable[T], int, T) -> Iterable[T]
    """ Lazily return the last x items from this iterable or default. """

    last_items = deque(iterable, maxlen=items)

    for _ in range(items - len(last_items)):
        yield default

    for y in last_items:
        yield y

# reduce is technically the last value of accumulate
# use ww.utils.EMPTY instead of EMPTY
# Put in the doc than scan=fold=accumulare and reduce=accumulate
# replace https://docs.python.org/3/library/itertools.html#itertools.accumulate
# that works only on Python 3.3 and doesn't have echo_start
# def accumulate(func, iterable, start=ww.utils.EMPTY, *, echo_start=True):
#     """
#     Scan higher-order function.
#     The first 3 positional arguments are alike to the ``functools.reduce``
#     signature. This function accepts an extra optional ``echo_start``
#     parameter that controls whether the first value should be in the output.
#     """
#     it = iter(iterable)
#     if start is ww.utils._EMPTY:
#         start = next(it)
#     if echo_start:
#         yield start
#     for item in it:
#         start = func(start, item)
# yield start
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号