Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd examples to the ParallelIterator documentation #467
Conversation
This comment has been minimized.
This comment has been minimized.
|
Thanks for the examples! However, this is surely going to collide in a big way with #466, which I just approved since it came in first. When that lands, please do compare and merge. Where your examples might show some distinctly interesting case, feel free to keep both, otherwise we should keep whichever example is most easily understood. The CI failure should be fixed by #468. |
cuviper
changed the title
Add examples to the ParrallelIterator documentation
Add examples to the ParallelIterator documentation
Nov 6, 2017
This comment has been minimized.
This comment has been minimized.
|
As for The primary motivating example of this is the There aren't many options to do this sort of thing manually -- e.g. maybe a |
Kerollmops
added some commits
Nov 4, 2017
Kerollmops
added some commits
Nov 9, 2017
This comment has been minimized.
This comment has been minimized.
|
Thanks for the indications. I have rebased my branch and done some improvements on the previously merged examples. I will improve some of the examples already present here and add examples to the Thank you for the details about theses functions. |
Kerollmops
added some commits
Nov 9, 2017
This comment has been minimized.
This comment has been minimized.
|
I have just added an example to the You can review this PR and merge it if you think it's good |
cuviper
requested changes
Nov 13, 2017
|
Mostly good, but a couple of the examples are depending on sequential order. Even if these happen to work in the rustdoc+CI environment, it's not a good example for documentation. |
| /// | ||
| /// let res: Vec<_> = receiver.iter().collect(); | ||
| /// | ||
| /// assert_eq!(&res[..], &[0, 1, 2, 3, 4]) |
This comment has been minimized.
This comment has been minimized.
cuviper
Nov 13, 2017
Member
I think you got lucky that this succeeded in CI, because the order of calling send in parallel is not guaranteed! Perhaps we should explicitly sort the res before comparing it.
This comment has been minimized.
This comment has been minimized.
| /// .reduce(|| (0, 0), // the "identity" is 0 in both columns | ||
| /// .par_iter() | ||
| /// .cloned() | ||
| /// .reduce(|| (0, 0), |
This comment has been minimized.
This comment has been minimized.
cuviper
Nov 13, 2017
Member
Why did you remove these comments? I disagree with your commit calling this less readable -- it's trying to explicitly explain the steps that go into a reduce.
| /// let sum = bytes.into_par_iter() | ||
| /// .fold(|| 0_u32, |a: u32, b: u8| a + (b as u32)) | ||
| /// .sum::<u32>(); | ||
| /// assert_eq!(sum, (0..22).sum()); // compare to sequential |
This comment has been minimized.
This comment has been minimized.
| /// ``` | ||
| /// use rayon::prelude::*; | ||
| /// fn factorial(n: u32) -> u32 { | ||
| /// | ||
| /// fn factorial(n: usize) -> usize { |
This comment has been minimized.
This comment has been minimized.
cuviper
Nov 13, 2017
Member
Why did you change the types? Idiomatic Rust is supposed to use explicitly-sized integers when you're not dealing with indexing or container sizes.
| /// | ||
| /// let sum: usize = a.par_iter().cloned().while_some().sum::<usize>(); | ||
| /// | ||
| /// assert_eq!(sum, 7); |
This comment has been minimized.
This comment has been minimized.
cuviper
Nov 13, 2017
Member
I think this test is getting lucky too, but perhaps we need to clarify the documented behavior. It halts as soon as a None is found anywhere, in arbitrary parallel order. You can't assume that it will see all of the sequentially-prior Some values before finding a None, and it may also visit Some values that are sequentially after a None. In the extreme, this while_some could produce nothing if it hit the None first, or it may even produce all of the Some items if it hit the None last!
This comment has been minimized.
This comment has been minimized.
Kerollmops
Nov 13, 2017
•
Author
Contributor
So what kind of example can I use here ? It's some sort of random that occurs...
This comment has been minimized.
This comment has been minimized.
cuviper
Nov 13, 2017
Member
Perhaps while_some().count() < a.len() is a better example? It's somewhat random which Some elements will make it through, but we can guarantee that it will be less than the total vector length.
You can make the same assertion about length with filter_map though - I'm not sure how to clearly demonstrate that while_some short-circuits. Maybe we could show an infinite source iterator getting stopped, like rayon::iter::repeat(None).while_some().count() == 0. Or for another example, check_while_some uses an atomic to count how many values were produced behind the scenes.
As a practical use-case, FromParallelIterator for Option and for Result each use while_some() to short-circuit their collection when they see a None or Err.
This comment has been minimized.
This comment has been minimized.
Kerollmops
Nov 14, 2017
Author
Contributor
Ok, so I will use the test code, it is clear and short.
Thank you for the explaination, really usefull and clear too.
Powerfull while_some method !
Kerollmops
added some commits
Nov 13, 2017
cuviper
approved these changes
Nov 14, 2017
This comment has been minimized.
This comment has been minimized.
|
Looks great, thanks! bors r+ |
bors bot
added a commit
that referenced
this pull request
Nov 14, 2017
This comment has been minimized.
This comment has been minimized.
Build failed |
This comment has been minimized.
This comment has been minimized.
Looks spurious. bors r+ |
Kerollmops commentedNov 5, 2017
•
edited
The last remaining examples to do are
map_withandfold_withthat I don't really understand.Could you please explain to me the mecanics ?