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 find_map with any, first, and last variants #627

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion src/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ pub trait ParallelIterator: Sized + Send {
/// just want the first match that discovered anywhere in the iterator,
/// `find_any` is a better choice.
///
/// # Exmaples
/// # Examples
///
/// ```
/// use rayon::prelude::*;
Expand Down Expand Up @@ -1551,6 +1551,99 @@ pub trait ParallelIterator: Sized + Send {
find_first_last::find_last(self, predicate)
}

/// Applies the given predicate to the items in the parallel iterator
/// and returns **any** non-None result of the map operation.
///
/// Once a non-None value is produced from the map operation, we will
/// attempt to stop processing the rest of the items in the iterator
/// as soon as possible.
///
/// Note that this method only returns **some** item in the parallel
/// iterator that is not None from the map predicate. The item returned
/// may not be the **first** non-None value produced in the parallel
/// sequence, since the entire sequence is mapped over in parallel.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let c = ["lol", "NaN", "5", "5"];
///
/// let first_number = c.par_iter().find_map_first(|s| s.parse().ok());
///
/// assert_eq!(first_number, Some(5));
/// ```
fn find_map_any<P, R>(self, predicate: P) -> Option<R>
where
P: Fn(Self::Item) -> Option<R> + Sync + Send,
R: Send,
{
self.filter_map(predicate).find_any(|_| true)
}

/// Applies the given predicate to the items in the parallel iterator and
/// returns the sequentially **first** non-None result of the map operation.
///
/// Once a non-None value is produced from the map operation, all attempts
/// to the right of the match will be stopped, while attempts to the left
/// must continue in case an earlier match is found.
///
/// Note that not all parallel iterators have a useful order, much like
/// sequential `HashMap` iteration, so "first" may be nebulous. If you
/// just want the first non-None value discovered anywhere in the iterator,
/// `find_map_any` is a better choice.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let c = ["lol", "NaN", "2", "5"];
///
/// let first_number = c.par_iter().find_map_first(|s| s.parse().ok());
///
/// assert_eq!(first_number, Some(2));
/// ```
fn find_map_first<P, R>(self, predicate: P) -> Option<R>
where
P: Fn(Self::Item) -> Option<R> + Sync + Send,
R: Send,
{
self.filter_map(predicate).find_first(|_| true)
}

/// Applies the given predicate to the items in the parallel iterator and
/// returns the sequentially **last** non-None result of the map operation.
///
/// Once a non-None value is produced from the map operation, all attempts
/// to the left of the match will be stopped, while attempts to the right
/// must continue in case a later match is found.
///
/// Note that not all parallel iterators have a useful order, much like
/// sequential `HashMap` iteration, so "first" may be nebulous. If you
/// just want the first non-None value discovered anywhere in the iterator,
/// `find_map_any` is a better choice.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let c = ["lol", "NaN", "2", "5"];
///
/// let first_number = c.par_iter().find_map_last(|s| s.parse().ok());
///
/// assert_eq!(first_number, Some(5));
/// ```
fn find_map_last<P, R>(self, predicate: P) -> Option<R>
where
P: Fn(Self::Item) -> Option<R> + Sync + Send,
R: Send,
{
self.filter_map(predicate).find_last(|_| true)
}

#[doc(hidden)]
#[deprecated(note = "parallel `find` does not search in order -- use `find_any`, \\
`find_first`, or `find_last`")]
Expand Down
2 changes: 1 addition & 1 deletion src/iter/plumbing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ such as string characters.
## What on earth is `ProducerCallback`?

We saw that when you call a parallel action method like
`par_iter.reduce()`, that will creating a "reducing" consumer and then
`par_iter.reduce()`, that will create a "reducing" consumer and then
invoke `par_iter.drive_unindexed()` (or `par_iter.drive()`) as
appropriate. This may create yet more consumers as we proceed up the
parallel iterator chain. But at some point we're going to get to the
Expand Down
44 changes: 44 additions & 0 deletions src/iter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,50 @@ pub fn find_first_or_last() {
assert_eq!(a.par_iter().position_last(|&x| x < 0), None);
}

#[test]
pub fn find_map_first_or_last_or_any() {
let mut a: Vec<i32> = vec![];

assert!(a.par_iter().find_map_any(half_if_positive).is_none());
assert!(a.par_iter().find_map_first(half_if_positive).is_none());
assert!(a.par_iter().find_map_last(half_if_positive).is_none());

a = (-1024..-3).collect();

assert!(a.par_iter().find_map_any(half_if_positive).is_none());
assert!(a.par_iter().find_map_first(half_if_positive).is_none());
assert!(a.par_iter().find_map_last(half_if_positive).is_none());

assert!(a.par_iter().find_map_any(half_if_negative).is_some());
assert_eq!(
a.par_iter().find_map_first(half_if_negative),
Some(-512_i32)
);
assert_eq!(a.par_iter().find_map_last(half_if_negative), Some(-2_i32));

a.append(&mut (2..1025).collect());

assert!(a.par_iter().find_map_any(half_if_positive).is_some());
assert_eq!(a.par_iter().find_map_first(half_if_positive), Some(1_i32));
assert_eq!(a.par_iter().find_map_last(half_if_positive), Some(512_i32));

fn half_if_positive(x: &i32) -> Option<i32> {
if *x > 0 {
Some(x / 2)
} else {
None
}
}

fn half_if_negative(x: &i32) -> Option<i32> {
if *x < 0 {
Some(x / 2)
} else {
None
}
}
}

#[test]
pub fn check_find_not_present() {
let counter = AtomicUsize::new(0);
Expand Down