Exemple #1
0
def running_sum(start=None):
    """Running sum co-routine.

    With no arguments, ``running_sum`` consumes values and returns the
    running sum of arguments sent to it:

    >>> rsum = running_sum()
    >>> rsum.send(1)
    1
    >>> [rsum.send(n) for n in (2, 3, 4)]
    [3, 6, 10]

    If optional argument ``start`` is given and is not None, it is used as
    the initial value for the running sum:

    >>> rsum = running_sum(9)
    >>> [rsum.send(n) for n in (1, 2, 3)]
    [10, 12, 15]

    """
    if start is None:
        partials = []
    else:
        partials = [start]
    x = (yield None)
    while True:
        add_partial(partials, x)
        x = (yield _builtin_sum(partials))
Exemple #2
0
def count(iterable=None):
    if isinstance(iterable, (Expr, core.FExpr)):
        return Expr(OpCodes.COUNT, (iterable, ))
    elif iterable is None:
        return Expr(OpCodes.COUNT0, ())
    else:
        return _builtin_sum((x is not None) for x in iterable)
Exemple #3
0
def count(iterable=None):
    if isinstance(iterable, Expr):
        return Expr(OpCodes.COUNT, iterable)
    elif iterable is None:
        return Expr(OpCodes.COUNT0)
    else:
        return _builtin_sum((x is not None) for x in iterable)
Exemple #4
0
def sum(iterable, start=0):
    if isinstance(iterable, (Expr, core.FExpr)):
        return Expr(OpCodes.SUM, (iterable, ))
    else:
        return _builtin_sum(iterable, start)
Exemple #5
0
def sum(iterable, start=0):
    if isinstance(iterable, Expr):
        return Expr(OpCodes.SUM, iterable)
    else:
        return _builtin_sum(iterable, start)