Example #1
0
        def create():
            def predicate(x: int) -> bool:
                if x < 4:
                    return x % 2 == 1
                else:
                    raise Exception(ex)

            return xs.pipe(_.last_or_default(0, predicate))
Example #2
0
    def last_or_default(source: Observable[Any]) -> Observable[Any]:
        """Return last or default element.

        Examples:
            >>> res = _last_or_default(source)

        Args:
            source: Observable sequence to get the last item from.

        Returns:
            Observable sequence containing the last element in the
            observable sequence.
        """

        if predicate:
            return source.pipe(
                ops.filter(predicate),
                ops.last_or_default(default_value),
            )

        return last_or_default_async(source, True, default_value)
Example #3
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(),
    )
Example #4
0
        def create() -> Observable[int]:
            def predicate(x: int) -> bool:
                return x % 2 == 1

            return xs.pipe(_.last_or_default(0, predicate))
Example #5
0
 def create():
     return xs.pipe(_.last_or_default(0))
Example #6
0
 def create() -> Observable[int]:
     return xs.pipe(_.last_or_default(0))
Example #7
0
        def create():
            def predicate(x: int) -> bool:
                return x > 10

            return xs.pipe(_.last_or_default(0, predicate))