Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/qpromise/filter.md
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/qpromise/filter.md
- This file was added.
1 | --- | ||||
---|---|---|---|---|---|
2 | title: .filter | ||||
3 | --- | ||||
4 | | ||||
5 | # QPromise::filter | ||||
6 | | ||||
7 | *Since: 0.4.0* | ||||
8 | | ||||
9 | ```cpp | ||||
10 | QPromise<Sequence<T>>::filter(Filter filterer) -> QPromise<Sequence<T>> | ||||
11 | | ||||
12 | // With: | ||||
13 | // - Sequence: STL compatible container (e.g. QVector, etc.) | ||||
14 | // - Filterer: Function(T value, int index) -> bool | ||||
15 | ``` | ||||
16 | | ||||
17 | ::: warning IMPORTANT | ||||
18 | This method only applies to promise with sequence value. | ||||
19 | ::: | ||||
20 | | ||||
21 | Iterates over all the promise values (i.e. `Sequence<T>`) and [filters the sequence](https://en.wikipedia.org/wiki/Filter_%28higher-order_function%29) to another using the given `filterer` function. If `filterer` returns `true`, a copy of the item is put in the `output` sequence, otherwise, the item will not appear in `output`. If `filterer` throws, `output` is rejected with the new exception. | ||||
22 | | ||||
23 | If `filterer` returns a promise (or `QFuture`), the `output` promise is delayed until all the promises are resolved. If any of the promises fail, `output` immediately rejects with the error of the promise that rejected, whether or not the other promises are resolved. | ||||
24 | | ||||
25 | ```cpp | ||||
26 | QPromise<QList<QUrl>> input = {...} | ||||
27 | | ||||
28 | auto output = input.filter([](const QUrl& url, ...) { | ||||
29 | return url.isValid(); // Keep only valid URLs | ||||
30 | }).filter([](const QUrl& url, ...) { | ||||
31 | return QPromise<bool>([&](auto resolve, auto reject) { | ||||
32 | // resolve(true) if `url` is reachable, else resolve(false) | ||||
33 | }); | ||||
34 | }); | ||||
35 | | ||||
36 | // 'output' resolves as soon as all promises returned by | ||||
37 | // 'filterer' are fulfilled or at least one is rejected. | ||||
38 | | ||||
39 | // 'output' type: QPromise<QList<QUrl>> | ||||
40 | output.then([](const QList<QUrl>& res) { | ||||
41 | // 'res' contains only reachable URLs | ||||
42 | }); | ||||
43 | ``` | ||||
44 | | ||||
45 | ::: tip NOTE | ||||
46 | The order of the output sequence values is guarantee to be the same as the original sequence, regardless of completion order of the promises returned by `filterer`. | ||||
47 | ::: | ||||
48 | | ||||
49 | See also: [`QtPromise::filter`](../helpers/filter.md) |