def create(): def func(acc, x): if acc is None: acc = 0 return acc + x return xs.pipe(_.scan(func))
def create() -> Observable[int]: def func(acc: int, x: int) -> int: if acc is None: acc = 0 return acc + x return xs.pipe(_.scan(accumulator=func))
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(), )
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
def create(): return xs.pipe(_.scan(lambda acc, x: acc + x, seed=seed))
def create() -> Observable[int]: return xs.pipe(_.scan(lambda acc, x: acc + x, seed))
def create(): def func(acc: int, x: int) -> int: return acc + x return never().pipe(_.scan(seed=seed, accumulator=func))
def create() -> Observable[int]: return never().pipe(_.scan(lambda acc, x: acc + x))