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 create(): def predicate(x): if x < 4: return False else: raise Exception(ex) return xs.pipe(ops.single_or_default(predicate, 0))
def create(): def predicate(x): return x % 2 == 1 return xs.pipe(ops.single_or_default(predicate, 0))
def create(): return xs.pipe(ops.single_or_default(None, 0))