Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rayon example with any and all #335

Merged
merged 4 commits into from Oct 30, 2017

Conversation

Projects
None yet
3 participants
@CJStadler
Copy link
Contributor

CJStadler commented Oct 15, 2017

fixes #331

edit: added fixes #311 clause to description to close the issue on merge.

@j-haj

This comment has been minimized.

Copy link
Contributor

j-haj commented Oct 16, 2017

Outstanding example! I think the description is especially well written. Just a minor fix on a missing word.


[![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]

The `rayon` provides a [`ParallelIterator`] trait which includes parallelized implementations of many of the methods from the standard library's [`Iterator`] trait.

This comment has been minimized.

@j-haj

j-haj Oct 16, 2017

Contributor

Missing "crate" in "The rayon [crate] provides..."


The `rayon` provides a [`ParallelIterator`] trait which includes parallelized implementations of many of the methods from the standard library's [`Iterator`] trait.

This example demonstrates using the [`any`] and [`all`] methods. [`any`] checks in parallel whether any element of the iterator matches the predicate, and returns as soon as one is found. [`all`] checks in parallel whether all elements of the iterator match the predicate, and returns as soon as a non-matching element is found.

This comment has been minimized.

@j-haj

j-haj Oct 16, 2017

Contributor

I think we should use rayon::any and rayon::all here, since there are also functions any and all in the standard library. The concern isn't so much clarity (I think this example is very well written) but more so about namespacing the function names in our links.

use rayon::prelude::*;
fn main() {

This comment has been minimized.

@j-haj

j-haj Oct 16, 2017

Contributor

There seems to be a mix of examples using a main() function and a those using error_chain. I'm not sure the Contributing guide is clear on this either, as it does not explicitly state contributions should use error_chain. On the other hand, there are no errors we need to worry about here, so maybe using error_chain unnecessarily complicates the examples.

@budziq what are your thoughts here? I am somewhat partial to using a consistent approach across examples.

This comment has been minimized.

@budziq

budziq Oct 16, 2017

Collaborator

@j-haj Thanks for taking this on 👍 .
You are right our note about error handling does not specifically state what is the correct course of action if there are not errors to handle.

The approach is as follows. If there are no errors to handle, there is no reason to increase the complexity of the example with run and error_chain and we just use plain main. We might be interested in adding this information to our contributing and error handling sections

This comment has been minimized.

@CJStadler

CJStadler Oct 16, 2017

Author Contributor

I was also wondering about this. Thanks!

@CJStadler

This comment has been minimized.

Copy link
Contributor Author

CJStadler commented Oct 17, 2017

Thanks for the review @j-haj!

@budziq
Copy link
Collaborator

budziq left a comment

Thanks for the PR @CJStadler!

It is almost perfect. I'd suggest only minor changes.


[![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]

The `rayon` crate provides a [`ParallelIterator`] trait which includes parallelized implementations of many of the methods from the standard library's [`Iterator`] trait.

This comment has been minimized.

@budziq

budziq Oct 17, 2017

Collaborator

I'd suggest to omit this first paragraph as the reader should already be familiar with rayon basics via the previous examples. On the other hand we might want to mention that rayon::any and rayon::all are parallel versions of their std counterparts.

This comment has been minimized.

@CJStadler

CJStadler Oct 19, 2017

Author Contributor

👍 I can mention the std counterparts in the second paragraph.

fn main() {
let mut vec = vec![2, 4, 6, 8];
let any_odd = vec.par_iter().any(|n| (*n % 2) != 0);

This comment has been minimized.

@budziq

budziq Oct 17, 2017

Collaborator

I'd suggest to write these just as a single line assertions

assert!(!vec.par_iter().any(|n| (*n % 2) != 0));

this would be slightly more concise

This comment has been minimized.

@CJStadler

CJStadler Oct 19, 2017

Author Contributor

Personally I think it's slightly more readable to break up the line and give this a name, but I'm happy to change it.

let all_even = vec.par_iter().all(|n| (*n % 2) == 0);
assert!(all_even);

This comment has been minimized.

@budziq

budziq Oct 17, 2017

Collaborator

how about we add a little flavor to the second predicate like some inequality?

 assert!(vec.par_iter().all(|n| *n < 10));
  vec.push(13);

This comment has been minimized.

@CJStadler

CJStadler Oct 19, 2017

Author Contributor

It seems like this loses some of the power of the example to demonstrate the relationship between the methods (when not any are odd then all are even, when any are odd then not all are even).

This comment has been minimized.

@budziq

budziq Oct 24, 2017

Collaborator

Imho the std examples for all/any are a little more expressive as these are showing inequality instead of just equality operators. We can always add another two asserts with different predicates

@@ -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] |
| [Check the elements of an array against a predicate in parallel][ex-rayon-any-all] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |

This comment has been minimized.

@budziq

budziq Oct 17, 2017

Collaborator

How about renaming the example to simply:
"Test in parallel if all or any collection elements match given predicate"

We are not limited to arrays here (and actually any collection/iterator viable for par_Iter will do)

This comment has been minimized.

@CJStadler

CJStadler Oct 19, 2017

Author Contributor

Definitely 👍

@budziq
Copy link
Collaborator

budziq left a comment

Only one minor nit left and we are ready to merge!

let all_even = vec.par_iter().all(|n| (*n % 2) == 0);
assert!(all_even);

This comment has been minimized.

@budziq

budziq Oct 24, 2017

Collaborator

Imho the std examples for all/any are a little more expressive as these are showing inequality instead of just equality operators. We can always add another two asserts with different predicates

vec.push(3);
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.

This comment has been minimized.

@budziq

budziq Oct 24, 2017

Collaborator

Imho the std examples for all/any are a little more expressive as these are showing inequality instead of just equality operators. We can always add another two asserts with different predicates. Please consider slightly modifying or expanding the predicates and we are ready to merge.

@CJStadler CJStadler force-pushed the CJStadler:rayon-any-all branch from 99ae3fd to cfaac04 Oct 29, 2017

@CJStadler

This comment has been minimized.

Copy link
Contributor Author

CJStadler commented Oct 29, 2017

Thanks @budziq! I added some examples with inequality.

@budziq budziq merged commit 705475c into rust-lang-nursery:master Oct 30, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@budziq

This comment has been minimized.

Copy link
Collaborator

budziq commented Oct 30, 2017

Nicely done @CJStadler !

Please also checkout #209 and comment there if you consent to repository re-license to CC0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.