Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/helpers/filter.md
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/helpers/filter.md
- This file was added.
1 | --- | ||||
---|---|---|---|---|---|
2 | title: filter | ||||
3 | --- | ||||
4 | | ||||
5 | # QtPromise::filter | ||||
6 | | ||||
7 | *Since: 0.4.0* | ||||
8 | | ||||
9 | ```cpp | ||||
10 | QtPromise::filter(Sequence<T> values, Filterer 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 | Iterates over `values` 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. | ||||
18 | | ||||
19 | 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. | ||||
20 | | ||||
21 | ```cpp | ||||
22 | auto output = QtPromise::filter(QVector{ | ||||
23 | QUrl("http://a..."), | ||||
24 | QUrl("http://b..."), | ||||
25 | QUrl("http://c...") | ||||
26 | }, [](const QUrl& url, ...) { | ||||
27 | return QPromise<bool>([&](auto resolve, auto reject) { | ||||
28 | // resolve(true) if 'url' is reachable, else resolve(false) | ||||
29 | // {...} | ||||
30 | }); | ||||
31 | }); | ||||
32 | | ||||
33 | // 'output' resolves as soon as all promises returned by | ||||
34 | // 'filterer' are fulfilled or at least one is rejected. | ||||
35 | | ||||
36 | // 'output' type: QPromise<QVector<QUrl>> | ||||
37 | output.then([](const QVector<QUrl>& res) { | ||||
38 | // 'res' contains only reachable URLs | ||||
39 | }); | ||||
40 | ``` | ||||
41 | | ||||
42 | ::: tip NOTE | ||||
43 | 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`. | ||||
44 | ::: | ||||
45 | | ||||
46 | See also: [`QPromise<T>::filter`](../qpromise/filter.md) |