|
1 | 1 | from asyncio import Future |
2 | | -from collections.abc import Callable |
3 | 2 | from typing import Any, TypeVar, Union, cast |
4 | 3 |
|
5 | 4 | from reactivex import Observable, from_, from_future |
6 | 5 | from reactivex import operators as ops |
| 6 | +from reactivex.internal import curry_flip |
7 | 7 | from reactivex.internal.basic import identity |
8 | 8 | from reactivex.typing import Mapper, MapperIndexed |
9 | 9 |
|
@@ -38,95 +38,98 @@ def projection(x: _T1, i: int) -> Observable[Any]: |
38 | 38 | ) |
39 | 39 |
|
40 | 40 |
|
| 41 | +@curry_flip |
41 | 42 | def flat_map_( |
| 43 | + source: Observable[_T1], |
42 | 44 | mapper: Mapper[_T1, Observable[_T2]] | None = None, |
43 | | -) -> Callable[[Observable[_T1]], Observable[_T2]]: |
44 | | - def flat_map(source: Observable[_T1]) -> Observable[_T2]: |
45 | | - """One of the Following: |
46 | | - Projects each element of an observable sequence to an observable |
47 | | - sequence and merges the resulting observable sequences into one |
48 | | - observable sequence. |
49 | | -
|
50 | | - Example: |
51 | | - >>> flat_map(source) |
52 | | -
|
53 | | - Args: |
54 | | - source: Source observable to flat map. |
55 | | -
|
56 | | - Returns: |
57 | | - An operator function that takes a source observable and returns |
58 | | - an observable sequence whose elements are the result of invoking |
59 | | - the one-to-many transform function on each element of the |
60 | | - input sequence . |
61 | | - """ |
62 | | - |
63 | | - if callable(mapper): |
64 | | - ret = _flat_map_internal(source, mapper=mapper) |
65 | | - else: |
66 | | - ret = _flat_map_internal(source, mapper=lambda _: mapper) |
| 45 | +) -> Observable[_T2]: |
| 46 | + """Projects each element of an observable sequence to an observable |
| 47 | + sequence and merges the resulting observable sequences into one |
| 48 | + observable sequence. |
| 49 | +
|
| 50 | + Examples: |
| 51 | + >>> source.pipe(flat_map(lambda x: of(x * 2))) |
| 52 | + >>> flat_map(lambda x: of(x * 2))(source) |
| 53 | +
|
| 54 | + Args: |
| 55 | + source: Source observable to flat map. |
| 56 | + mapper: Transform function to apply to each element. |
67 | 57 |
|
68 | | - return ret |
| 58 | + Returns: |
| 59 | + An observable sequence whose elements are the result of invoking |
| 60 | + the one-to-many transform function on each element of the |
| 61 | + input sequence. |
| 62 | + """ |
69 | 63 |
|
70 | | - return flat_map |
| 64 | + if callable(mapper): |
| 65 | + ret = _flat_map_internal(source, mapper=mapper) |
| 66 | + else: |
| 67 | + ret = _flat_map_internal(source, mapper=lambda _: mapper) |
71 | 68 |
|
| 69 | + return ret |
72 | 70 |
|
| 71 | + |
| 72 | +@curry_flip |
73 | 73 | def flat_map_indexed_( |
| 74 | + source: Observable[Any], |
74 | 75 | mapper_indexed: Any | None = None, |
75 | | -) -> Callable[[Observable[Any]], Observable[Any]]: |
76 | | - def flat_map_indexed(source: Observable[Any]) -> Observable[Any]: |
77 | | - """One of the Following: |
78 | | - Projects each element of an observable sequence to an observable |
79 | | - sequence and merges the resulting observable sequences into one |
80 | | - observable sequence. |
81 | | -
|
82 | | - Example: |
83 | | - >>> flat_map_indexed(source) |
84 | | -
|
85 | | - Args: |
86 | | - source: Source observable to flat map. |
87 | | -
|
88 | | - Returns: |
89 | | - An observable sequence whose elements are the result of invoking |
90 | | - the one-to-many transform function on each element of the input |
91 | | - sequence. |
92 | | - """ |
93 | | - |
94 | | - if callable(mapper_indexed): |
95 | | - ret = _flat_map_internal(source, mapper_indexed=mapper_indexed) |
96 | | - else: |
97 | | - ret = _flat_map_internal(source, mapper=lambda _: mapper_indexed) |
98 | | - return ret |
| 76 | +) -> Observable[Any]: |
| 77 | + """Projects each element of an observable sequence to an observable |
| 78 | + sequence and merges the resulting observable sequences into one |
| 79 | + observable sequence. |
99 | 80 |
|
100 | | - return flat_map_indexed |
| 81 | + Examples: |
| 82 | + >>> source.pipe(flat_map_indexed(lambda x, i: of(x * i))) |
| 83 | + >>> flat_map_indexed(lambda x, i: of(x * i))(source) |
101 | 84 |
|
| 85 | + Args: |
| 86 | + source: Source observable to flat map. |
| 87 | + mapper_indexed: Transform function with index to apply to each element. |
102 | 88 |
|
| 89 | + Returns: |
| 90 | + An observable sequence whose elements are the result of invoking |
| 91 | + the one-to-many transform function on each element of the input |
| 92 | + sequence. |
| 93 | + """ |
| 94 | + |
| 95 | + if callable(mapper_indexed): |
| 96 | + ret = _flat_map_internal(source, mapper_indexed=mapper_indexed) |
| 97 | + else: |
| 98 | + ret = _flat_map_internal(source, mapper=lambda _: mapper_indexed) |
| 99 | + return ret |
| 100 | + |
| 101 | + |
| 102 | +@curry_flip |
103 | 103 | def flat_map_latest_( |
| 104 | + source: Observable[_T1], |
104 | 105 | mapper: Mapper[_T1, Union[Observable[_T2], "Future[_T2]"]], |
105 | | -) -> Callable[[Observable[_T1]], Observable[_T2]]: |
106 | | - def flat_map_latest(source: Observable[_T1]) -> Observable[_T2]: |
107 | | - """Projects each element of an observable sequence into a new |
108 | | - sequence of observable sequences by incorporating the element's |
109 | | - index and then transforms an observable sequence of observable |
110 | | - sequences into an observable sequence producing values only |
111 | | - from the most recent observable sequence. |
112 | | -
|
113 | | - Args: |
114 | | - source: Source observable to flat map latest. |
115 | | -
|
116 | | - Returns: |
117 | | - An observable sequence whose elements are the result of |
118 | | - invoking the transform function on each element of source |
119 | | - producing an observable of Observable sequences and that at |
120 | | - any point in time produces the elements of the most recent |
121 | | - inner observable sequence that has been received. |
122 | | - """ |
123 | | - |
124 | | - return source.pipe( |
125 | | - ops.map(mapper), |
126 | | - ops.switch_latest(), |
127 | | - ) |
| 106 | +) -> Observable[_T2]: |
| 107 | + """Projects each element of an observable sequence into a new |
| 108 | + sequence of observable sequences by incorporating the element's |
| 109 | + index and then transforms an observable sequence of observable |
| 110 | + sequences into an observable sequence producing values only |
| 111 | + from the most recent observable sequence. |
| 112 | +
|
| 113 | + Examples: |
| 114 | + >>> source.pipe(flat_map_latest(lambda x: of(x * 2))) |
| 115 | + >>> flat_map_latest(lambda x: of(x * 2))(source) |
| 116 | +
|
| 117 | + Args: |
| 118 | + source: Source observable to flat map latest. |
| 119 | + mapper: Transform function to apply to each element. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + An observable sequence whose elements are the result of |
| 123 | + invoking the transform function on each element of source |
| 124 | + producing an observable of Observable sequences and that at |
| 125 | + any point in time produces the elements of the most recent |
| 126 | + inner observable sequence that has been received. |
| 127 | + """ |
128 | 128 |
|
129 | | - return flat_map_latest |
| 129 | + return source.pipe( |
| 130 | + ops.map(mapper), |
| 131 | + ops.switch_latest(), |
| 132 | + ) |
130 | 133 |
|
131 | 134 |
|
132 | 135 | __all__ = ["flat_map_", "flat_map_latest_", "flat_map_indexed_"] |
0 commit comments