Add rayon example with any and all

Add rayon example with any and all
This commit is contained in:
Chris Stadler 2017-10-30 12:10:53 -04:00 committed by Michał Budzyński
parent 828d2ca1cc
commit 705475c457
2 changed files with 40 additions and 1 deletions

View file

@ -3,6 +3,7 @@
| Recipe | Crates | Categories |
|--------|--------|------------|
| [Mutate the elements of an array in parallel][ex-rayon-iter-mut] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
| [Test in parallel if any or all elements of a collection match a given predicate][ex-rayon-any-all] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
| [Search items using given predicate in parallel][ex-rayon-parallel-search] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
| [Sort a vector in parallel][ex-rayon-parallel-sort] | [![rayon-badge]][rayon] [![rand-badge]][rand] | [![cat-concurrency-badge]][cat-concurrency] |
| [Map-reduce in parallel][ex-rayon-map-reduce] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
@ -35,6 +36,36 @@ fn main() {
}
```
[ex-rayon-any-all]: #ex-rayon-any-all
<a name="ex-rayon-any-all"></a>
## Test in parallel if any or all elements of a collection match a given predicate
[![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]
This example demonstrates using the [`rayon::any`] and [`rayon::all`] methods, which are parallelized counterparts to [`std::any`] and [`std::all`]. [`rayon::any`] checks in parallel whether any element of the iterator matches the predicate, and returns as soon as one is found. [`rayon::all`] checks in parallel whether all elements of the iterator match the predicate, and returns as soon as a non-matching element is found.
```rust
extern crate rayon;
use rayon::prelude::*;
fn main() {
let mut vec = vec![2, 4, 6, 8];
assert!(!vec.par_iter().any(|n| (*n % 2) != 0)); // None are odd.
assert!(vec.par_iter().all(|n| (*n % 2) == 0)); // All are even.
assert!(!vec.par_iter().any(|n| *n > 8 )); // None are greater than 8.
assert!(vec.par_iter().all(|n| *n <= 8 )); // All are less than or equal to 8.
vec.push(9);
assert!(vec.par_iter().any(|n| (*n % 2) != 0)); // At least 1 is odd.
assert!(!vec.par_iter().all(|n| (*n % 2) == 0)); // Not all are even.
assert!(vec.par_iter().any(|n| *n > 8 )); // At least 1 is greater than 8.
assert!(!vec.par_iter().all(|n| *n <= 8 )); // Not all are less than or equal to 8.
}
```
[ex-rayon-parallel-sort]: #ex-rayon-parallel-sort
<a name="ex-rayon-parallel-sort"></a>
## Sort a vector in parallel
@ -519,6 +550,12 @@ fn run() -> Result<()> {
[`rayon::sum`]: https://docs.rs/rayon/*/rayon/iter/trait.ParallelIterator.html#method.sum
[`std::find`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find
[`Walkdir::new`]: https://docs.rs/walkdir/1.0.7/walkdir/struct.WalkDir.html#method.new
[`ParallelIterator`]: https://docs.rs/rayon/*/rayon/iter/trait.ParallelIterator.html
[`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
[`rayon::any`]: https://docs.rs/rayon/*/rayon/iter/trait.ParallelIterator.html#method.any
[`rayon::all`]: https://docs.rs/rayon/*/rayon/iter/trait.ParallelIterator.html#method.all
[`std::any`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.any
[`std::all`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.all
<!-- Other Reference -->

View file

@ -68,6 +68,7 @@ community. It needs and welcomes help. For details see
| Recipe | Crates | Categories |
|--------|--------|------------|
| [Mutate the elements of an array in parallel][ex-rayon-iter-mut] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
| [Test in parallel if any or all elements of a collection match a given predicate][ex-rayon-any-all] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
| [Search items using given predicate in parallel][ex-rayon-parallel-search] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
| [Sort a vector in parallel][ex-rayon-parallel-sort] | [![rayon-badge]][rayon] [![rand-badge]][rand] | [![cat-concurrency-badge]][cat-concurrency] |
| [Map-reduce in parallel][ex-rayon-map-reduce] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
@ -204,6 +205,7 @@ community. It needs and welcomes help. For details see
[ex-rand-range]: basics.html#ex-rand-range
[ex-random-port-tcp]: net.html#ex-random-port-tcp
[ex-rayon-iter-mut]: concurrency.html#ex-rayon-iter-mut
[ex-rayon-any-all]: concurrency.html#ex-rayon-any-all
[ex-rayon-map-reduce]: concurrency.html#ex-rayon-map-reduce
[ex-rayon-parallel-search]: concurrency.html#ex-rayon-parallel-search
[ex-rayon-parallel-sort]: concurrency.html#ex-rayon-parallel-sort