@@ -1504,7 +1504,7 @@ pub trait ParallelIterator: Sized + Send {
1504
1504
/// just want the first match that discovered anywhere in the iterator,
1505
1505
/// `find_any` is a better choice.
1506
1506
///
1507
- /// # Exmaples
1507
+ /// # Examples
1508
1508
///
1509
1509
/// ```
1510
1510
/// use rayon::prelude::*;
@@ -1551,6 +1551,99 @@ pub trait ParallelIterator: Sized + Send {
1551
1551
find_first_last:: find_last ( self , predicate)
1552
1552
}
1553
1553
1554
+ /// Applies the given predicate to the items in the parallel iterator
1555
+ /// and returns **any** non-None result of the map operation.
1556
+ ///
1557
+ /// Once a non-None value is produced from the map operation, we will
1558
+ /// attempt to stop processing the rest of the items in the iterator
1559
+ /// as soon as possible.
1560
+ ///
1561
+ /// Note that this method only returns **some** item in the parallel
1562
+ /// iterator that is not None from the map predicate. The item returned
1563
+ /// may not be the **first** non-None value produced in the parallel
1564
+ /// sequence, since the entire sequence is mapped over in parallel.
1565
+ ///
1566
+ /// # Examples
1567
+ ///
1568
+ /// ```
1569
+ /// use rayon::prelude::*;
1570
+ ///
1571
+ /// let c = ["lol", "NaN", "5", "5"];
1572
+ ///
1573
+ /// let first_number = c.par_iter().find_map_first(|s| s.parse().ok());
1574
+ ///
1575
+ /// assert_eq!(first_number, Some(5));
1576
+ /// ```
1577
+ fn find_map_any < P , R > ( self , predicate : P ) -> Option < R >
1578
+ where
1579
+ P : Fn ( Self :: Item ) -> Option < R > + Sync + Send ,
1580
+ R : Send ,
1581
+ {
1582
+ self . filter_map ( predicate) . find_any ( |_| true )
1583
+ }
1584
+
1585
+ /// Applies the given predicate to the items in the parallel iterator and
1586
+ /// returns the sequentially **first** non-None result of the map operation.
1587
+ ///
1588
+ /// Once a non-None value is produced from the map operation, all attempts
1589
+ /// to the right of the match will be stopped, while attempts to the left
1590
+ /// must continue in case an earlier match is found.
1591
+ ///
1592
+ /// Note that not all parallel iterators have a useful order, much like
1593
+ /// sequential `HashMap` iteration, so "first" may be nebulous. If you
1594
+ /// just want the first non-None value discovered anywhere in the iterator,
1595
+ /// `find_map_any` is a better choice.
1596
+ ///
1597
+ /// # Examples
1598
+ ///
1599
+ /// ```
1600
+ /// use rayon::prelude::*;
1601
+ ///
1602
+ /// let c = ["lol", "NaN", "2", "5"];
1603
+ ///
1604
+ /// let first_number = c.par_iter().find_map_first(|s| s.parse().ok());
1605
+ ///
1606
+ /// assert_eq!(first_number, Some(2));
1607
+ /// ```
1608
+ fn find_map_first < P , R > ( self , predicate : P ) -> Option < R >
1609
+ where
1610
+ P : Fn ( Self :: Item ) -> Option < R > + Sync + Send ,
1611
+ R : Send ,
1612
+ {
1613
+ self . filter_map ( predicate) . find_first ( |_| true )
1614
+ }
1615
+
1616
+ /// Applies the given predicate to the items in the parallel iterator and
1617
+ /// returns the sequentially **last** non-None result of the map operation.
1618
+ ///
1619
+ /// Once a non-None value is produced from the map operation, all attempts
1620
+ /// to the left of the match will be stopped, while attempts to the right
1621
+ /// must continue in case a later match is found.
1622
+ ///
1623
+ /// Note that not all parallel iterators have a useful order, much like
1624
+ /// sequential `HashMap` iteration, so "first" may be nebulous. If you
1625
+ /// just want the first non-None value discovered anywhere in the iterator,
1626
+ /// `find_map_any` is a better choice.
1627
+ ///
1628
+ /// # Examples
1629
+ ///
1630
+ /// ```
1631
+ /// use rayon::prelude::*;
1632
+ ///
1633
+ /// let c = ["lol", "NaN", "2", "5"];
1634
+ ///
1635
+ /// let first_number = c.par_iter().find_map_last(|s| s.parse().ok());
1636
+ ///
1637
+ /// assert_eq!(first_number, Some(5));
1638
+ /// ```
1639
+ fn find_map_last < P , R > ( self , predicate : P ) -> Option < R >
1640
+ where
1641
+ P : Fn ( Self :: Item ) -> Option < R > + Sync + Send ,
1642
+ R : Send ,
1643
+ {
1644
+ self . filter_map ( predicate) . find_last ( |_| true )
1645
+ }
1646
+
1554
1647
#[ doc( hidden) ]
1555
1648
#[ deprecated( note = "parallel `find` does not search in order -- use `find_any`, \\
1556
1649
`find_first`, or `find_last`" ) ]
0 commit comments