def _single_or_default(predicate: Predicate = None, default_value: Any = None) -> Observable: """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 pipe(ops.filter(predicate), ops.single_or_default(None, default_value)) else: return _single_or_default_async(True, default_value)
def _single_or_default(predicate: Optional[Predicate] = None, default_value: Any = None) -> Observable: """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 pipe(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))