def buffer_toggle_( openings: Observable[Any], closing_mapper: Callable[[Any], Observable[Any]] ) -> Callable[[Observable[_T]], Observable[List[_T]]]: return compose( ops.window_toggle(openings, closing_mapper), ops.flat_map(ops.to_list()), )
def single_or_default_( predicate: Optional[Predicate[_T]] = None, default_value: _T = None ) -> Callable[[Observable[_T]], Observable[_T]]: """Returns the only element of an observable sequence that matches the predicate, or a default value if no such element exists this method reports an exception if there is more than one element in the observable sequence. Examples: >>> res = single_or_default() >>> res = single_or_default(lambda x: x == 42) >>> res = single_or_default(lambda x: x == 42, 0) >>> res = single_or_default(None, 0) Args: predicate -- [Optional] A predicate function to evaluate for elements in the source sequence. default_value -- [Optional] The default value if the index is outside the bounds of the source sequence. Returns: An observable Sequence containing the single element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists. """ if predicate: return compose( ops.filter(predicate), ops.single_or_default(None, default_value) ) else: return single_or_default_async_(True, default_value)
def buffer_when_( closing_mapper: Callable[[], Observable[Any]] ) -> Callable[[Observable[_T]], Observable[List[_T]]]: return compose( ops.window_when(closing_mapper), ops.flat_map(ops.to_list()), )
def first_or_default_( predicate: Optional[Predicate[_T]] = None, default_value: Optional[_T] = None ) -> Callable[[Observable[_T]], Observable[_T]]: """Returns the first element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists. Examples: >>> res = source.first_or_default() >>> res = source.first_or_default(lambda x: x > 3) >>> res = source.first_or_default(lambda x: x > 3, 0) >>> res = source.first_or_default(None, 0) Args: source -- Observable sequence. predicate -- [optional] A predicate function to evaluate for elements in the source sequence. default_value -- [Optional] The default value if no such element exists. If not specified, defaults to None. Returns: A function that takes an observable source and reutrn an observable sequence containing the first element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists. """ if predicate: return compose(ops.filter(predicate), ops.first_or_default(None, default_value)) return first_or_default_async_(True, default_value)
def buffer_( boundaries: Observable[Any], ) -> Callable[[Observable[_T]], Observable[List[_T]]]: return compose( ops.window(boundaries), ops.flat_map(ops.to_list()), )
def first_( predicate: Optional[Predicate[_T]] = None, ) -> Callable[[Observable[_T]], Observable[_T]]: """Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence. Examples: >>> res = res = first()(source) >>> res = res = first(lambda x: x > 3)(source) Args: predicate -- [Optional] A predicate function to evaluate for elements in the source sequence. Returns: A function that takes an observable source and returns an observable sequence containing the first element in the observable sequence that satisfies the condition in the predicate if provided, else the first item in the sequence. """ if predicate: return compose(ops.filter(predicate), ops.first()) return first_or_default_async_(False)
def sum_( key_mapper: Optional[Mapper[Any, float]] = None ) -> Callable[[Observable[Any]], Observable[float]]: if key_mapper: return compose(ops.map(key_mapper), ops.sum()) def accumulator(prev: float, cur: float) -> float: return prev + cur return ops.reduce(seed=0, accumulator=accumulator)
def map_indexed_( mapper_indexed: Optional[MapperIndexed[_T1, _T2]] = None ) -> Callable[[Observable[_T1]], Observable[_T2]]: def _identity(value: _T1, _: int) -> _T2: return cast(_T2, value) _mapper_indexed = mapper_indexed or cast(typing.MapperIndexed[_T1, _T2], _identity) return compose( ops.zip_with_iterable(infinite()), ops.starmap_indexed(_mapper_indexed), )
def buffer_with_time_( timespan: typing.RelativeTime, timeshift: Optional[typing.RelativeTime] = None, scheduler: Optional[abc.SchedulerBase] = None, ) -> Callable[[Observable[_T]], Observable[List[_T]]]: if not timeshift: timeshift = timespan return compose( ops.window_with_time(timespan, timeshift, scheduler), ops.flat_map(ops.to_list()), )
def contains_( value: _T, comparer: Optional[typing.Comparer[_T]] = None ) -> Callable[[Observable[_T]], Observable[bool]]: comparer_ = comparer or default_comparer def predicate(v: _T) -> bool: return comparer_(v, value) return compose( ops.filter(predicate), ops.some(), )
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 all_( predicate: Predicate[_T] ) -> Callable[[Observable[_T]], Observable[bool]]: def filter(v: _T): return not predicate(v) def mapping(b: bool) -> bool: return not b return compose( ops.filter(filter), ops.some(), ops.map(mapping), )
def is_empty_() -> Callable[[Observable[Any]], Observable[bool]]: """Determines whether an observable sequence is empty. Returns: An observable sequence containing a single element determining whether the source sequence is empty. """ def mapper(b: bool) -> bool: return not b return compose( ops.some(), ops.map(mapper), )
def count_( predicate: Optional[Predicate[_T]] = None, ) -> Callable[[Observable[_T]], Observable[int]]: if predicate: return compose( ops.filter(predicate), ops.count(), ) def reducer(n: int, _: _T) -> int: return n + 1 counter = ops.reduce(reducer, seed=0) return counter
def share_() -> Callable[[Observable[_TSource]], Observable[_TSource]]: """Share a single subscription among multple observers. Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one Subscriber this Observable will be subscribed and emitting data. When all subscribers have unsubscribed it will unsubscribe from the source Observable. This is an alias for a composed publish() and ref_count(). """ return compose( ops.publish(), ops.ref_count(), )
def skip_while_indexed_( predicate: typing.PredicateIndexed[_T], ) -> Callable[[Observable[_T]], Observable[_T]]: def indexer(x: _T, i: int) -> Tuple[_T, int]: return (x, i) def skipper(x: Tuple[_T, int]) -> bool: return predicate(*x) def mapper(x: Tuple[_T, int]) -> _T: return x[0] return compose( ops.map_indexed(indexer), ops.skip_while(skipper), ops.map(mapper), )
def max_( comparer: Optional[Comparer[_T]] = None, ) -> Callable[[Observable[_T]], Observable[_T]]: """Returns the maximum value in an observable sequence according to the specified comparer. Examples: >>> op = max() >>> op = max(lambda x, y: x.value - y.value) Args: comparer: [Optional] Comparer used to compare elements. Returns: An operator function that takes an observable source and returns an observable sequence containing a single element with the maximum element in the source sequence. """ return compose( ops.max_by(identity, comparer), ops.map(first_only), )
def min_( comparer: Optional[Comparer[_T]] = None, ) -> Callable[[Observable[_T]], Observable[_T]]: """The `min` operator. Returns the minimum element in an observable sequence according to the optional comparer else a default greater than less than check. Examples: >>> res = source.min() >>> res = source.min(lambda x, y: x.value - y.value) Args: comparer: [Optional] Comparer used to compare elements. Returns: An observable sequence containing a single element with the minimum element in the source sequence. """ return compose( ops.min_by(identity, comparer), ops.map(first_only), )