Пример #1
0
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)
Пример #2
0
    def test_first_async_one(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(150, 1), on_next(210, 2),
                                             on_completed(250))
        res = scheduler.start(lambda: xs.pipe(ops.first()))

        assert res.messages == [on_next(210, 2), on_completed(210)]
        assert xs.subscriptions == [subscribe(200, 210)]
Пример #3
0
        def create():
            def predicate(x):
                if x < 4:
                    return False
                else:
                    raise Exception(ex)

            return xs.pipe(ops.first(predicate))
Пример #4
0
    def test_first_async_error(self):
        ex = "ex"
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(150, 1),
                                             on_error(210, ex))
        res = scheduler.start(lambda: xs.pipe(ops.first()))

        assert res.messages == [on_error(210, ex)]
        assert xs.subscriptions == [subscribe(200, 210)]
Пример #5
0
 def create():
     return xs.pipe(ops.first(lambda x: x > 10))
Пример #6
0
 def create():
     return xs.pipe(ops.first(lambda x: x % 2 == 1))
Пример #7
0
 def create():
     return xs.pipe(ops.first())
Пример #8
0
 def test_run_from_first(self):
     result = reactivex.from_([1, 2, 3]).pipe(ops.first()).run()
     assert result == 1