Esempio n. 1
0
    def last(source: Observable[_T]) -> Observable[Any]:
        """Partially applied last operator.

        Returns the last element of an observable sequence that
        satisfies the condition in the predicate if specified, else
        the last element.

        Examples:
            >>> res = last(source)

        Args:
            source: Source observable to get last item from.

        Returns:
            An observable sequence containing the last element in the
            observable sequence that satisfies the condition in the
            predicate.
        """

        if predicate:
            return source.pipe(
                operators.filter(predicate),
                operators.last(),
            )

        return last_or_default_async(source, False)
Esempio n. 2
0
        def create():
            def predicate(x):
                if x < 4:
                    return x % 2 == 1
                else:
                    raise Exception(ex)

            return xs.pipe(_.last(predicate))
Esempio n. 3
0
    def average(source: Observable[Any]) -> Observable[float]:
        """Partially applied average operator.

        Computes the average of an observable sequence of values that
        are in the sequence or obtained by invoking a transform
        function on each element of the input sequence if present.

        Examples:
            >>> res = average(source)

        Args:
            source: Source observable to average.

        Returns:
            An observable sequence containing a single element with the
            average of the sequence of values.
        """

        key_mapper_: typing.Mapper[_T, float] = key_mapper or (
            lambda x: float(cast(Any, x)))

        def accumulator(prev: AverageValue, cur: float) -> AverageValue:
            return AverageValue(sum=prev.sum + cur, count=prev.count + 1)

        def mapper(s: AverageValue) -> float:
            if s.count == 0:
                raise Exception("The input sequence was empty")

            return s.sum / float(s.count)

        seed = AverageValue(sum=0, count=0)

        ret = source.pipe(
            operators.map(key_mapper_),
            operators.scan(accumulator, seed),
            operators.last(),
            operators.map(mapper),
        )
        return ret
Esempio n. 4
0
def reduce_(
    accumulator: Accumulator[_TState, _T],
    seed: Union[_TState, Type[NotSet]] = NotSet
) -> Callable[[Observable[_T]], Observable[Any]]:
    """Applies an accumulator function over an observable sequence,
    returning the result of the aggregation as a single element in the
    result sequence. The specified seed value is used as the initial
    accumulator value.

    For aggregation behavior with incremental intermediate results, see
    `scan()`.

    Examples:
        >>> res = reduce(lambda acc, x: acc + x)
        >>> res = reduce(lambda acc, x: acc + x, 0)

    Args:
        accumulator: An accumulator function to be
            invoked on each element.
        seed: Optional initial accumulator value.

    Returns:
        An operator function that takes an observable source and returns
        an observable sequence containing a single element with the
        final accumulator value.
    """
    if seed is not NotSet:
        seed_: _TState = cast(_TState, seed)
        scanner = ops.scan(accumulator, seed=seed_)
        return compose(
            scanner,
            ops.last_or_default(default_value=seed_),
        )

    return compose(
        ops.scan(accumulator),
        ops.last(),
    )
Esempio n. 5
0
        def create():
            def predicate(x):
                return x % 2 == 1

            return xs.pipe(_.last(predicate))
Esempio n. 6
0
 def create():
     return xs.pipe(_.last())