From ce3dd7fbaccb77511d2a178cf6a469507a0bbb6c Mon Sep 17 00:00:00 2001 From: oddg Date: Sat, 20 Jan 2018 14:42:37 -0800 Subject: [PATCH 1/5] Add documentation example for parallel iterator map_with method --- src/iter/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 022a984e4..0f7201c77 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -266,6 +266,23 @@ pub trait ParallelIterator: Sized + Send { /// The `init` value will be cloned only as needed to be paired with /// the group of items in each rayon job. It does not require the type /// to be `Sync`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::mpsc::channel; + /// use rayon::prelude::*; + /// + /// let (sender, receiver) = channel(); + /// + /// let mut par_iter = (0..5).into_par_iter().map_with(sender, |s, x| { s.send(x).unwrap(); x }); + /// let a: Vec<_> = par_iter.collect(); + /// + /// let mut b: Vec<_> = receiver.iter().collect(); + /// b.sort(); + /// + /// assert_eq!(a, b); + /// ``` fn map_with(self, init: T, map_op: F) -> MapWith where F: Fn(&mut T, Self::Item) -> R + Sync + Send, T: Send + Clone, From 5871ba0339efba6bf322c553f2b309d4a8a033b1 Mon Sep 17 00:00:00 2001 From: oddg Date: Sat, 20 Jan 2018 15:09:59 -0800 Subject: [PATCH 2/5] Add documentation example for parallel iterator reduce_with method --- src/iter/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 0f7201c77..5a7f891b7 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -464,7 +464,7 @@ pub trait ParallelIterator: Sized + Send { /// to produce something that represents the zero for your type /// (but consider just calling `sum()` in that case). /// - /// Example: + /// # Examples /// /// ``` /// // Iterate over a sequence of pairs `(x0, y0), ..., (xN, yN)` @@ -501,6 +501,18 @@ pub trait ParallelIterator: Sized + Send { /// efficient. If possible, it is better to call `reduce()`, which /// requires an identity element. /// + /// # Examples + /// + /// ``` + /// use rayon::prelude::*; + /// let sums = [(0, 1), (5, 6), (16, 2), (8, 9)] + /// .par_iter() // iterating over &(i32, i32) + /// .cloned() // iterating over (i32, i32) + /// .reduce_with(|a, b| (a.0 + b.0, a.1 + b.1)) + /// .unwrap(); + /// assert_eq!(sums, (0 + 5 + 16 + 8, 1 + 6 + 2 + 9)); + /// ``` + /// /// **Note:** unlike a sequential `fold` operation, the order in /// which `op` will be applied to reduce the result is not fully /// specified. So `op` should be [associative] or else the results From 88615ad905ff800894ed0a418bf57dcc1ef1b1c2 Mon Sep 17 00:00:00 2001 From: oddg Date: Sat, 20 Jan 2018 15:23:28 -0800 Subject: [PATCH 3/5] Formatting --- src/iter/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 5a7f891b7..102c443d7 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -440,7 +440,7 @@ pub trait ParallelIterator: Sized + Send { /// An adaptor that flattens iterable `Item`s into one large iterator /// - /// Example: + /// # Examples /// /// ``` /// use rayon::prelude::*; @@ -893,7 +893,7 @@ pub trait ParallelIterator: Sized + Send { /// specified, so if the `Ord` impl is not truly associative, then /// the results are not deterministic. /// - /// # Exmaples + /// # Examples /// /// ``` /// use rayon::prelude::*; From d2c68cfe55f399f80c4fe2fe40d5d14faad2278e Mon Sep 17 00:00:00 2001 From: oddg Date: Sat, 20 Jan 2018 15:28:36 -0800 Subject: [PATCH 4/5] Add documentation example for parallel iterator fold_with method --- src/iter/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 102c443d7..9bc8218ba 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -680,6 +680,19 @@ pub trait ParallelIterator: Sized + Send { /// This works essentially like `fold(|| init.clone(), fold_op)`, except /// it doesn't require the `init` type to be `Sync`, nor any other form /// of added synchronization. + /// + /// # Examples + /// + /// ``` + /// use rayon::prelude::*; + /// + /// let bytes = 0..22_u8; + /// let sum = bytes.into_par_iter() + /// .fold_with(0_u32, |a: u32, b: u8| a + (b as u32)) + /// .sum::(); + /// + /// assert_eq!(sum, (0..22).sum()); // compare to sequential + /// ``` fn fold_with(self, init: T, fold_op: F) -> FoldWith where F: Fn(T, Self::Item) -> T + Sync + Send, T: Send + Clone From 89363221af6d2b77f2d0a2affc4a0f7a62810c6d Mon Sep 17 00:00:00 2001 From: oddg Date: Tue, 23 Jan 2018 22:17:58 -0800 Subject: [PATCH 5/5] Format and comment the example for method map_with of the ParallelIterator --- src/iter/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 9bc8218ba..60e1b769b 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -275,10 +275,16 @@ pub trait ParallelIterator: Sized + Send { /// /// let (sender, receiver) = channel(); /// - /// let mut par_iter = (0..5).into_par_iter().map_with(sender, |s, x| { s.send(x).unwrap(); x }); - /// let a: Vec<_> = par_iter.collect(); - /// - /// let mut b: Vec<_> = receiver.iter().collect(); + /// let a: Vec<_> = (0..5) + /// .into_par_iter() // iterating over i32 + /// .map_with(sender, |s, x| { + /// s.send(x).unwrap(); // sending i32 values through the channel + /// x // returning i32 + /// }) + /// .collect(); // collecting the returned values into a vector + /// + /// let mut b: Vec<_> = receiver.iter() // iterating over the values in the channel + /// .collect(); // and collecting them /// b.sort(); /// /// assert_eq!(a, b);