@@ -1664,6 +1664,31 @@ pub trait Iterator {
1664
1664
. map ( |( _, x) | x)
1665
1665
}
1666
1666
1667
+ /// Returns the element that gives the maximum value with respect to the
1668
+ /// specified comparison function.
1669
+ ///
1670
+ /// Returns the rightmost element if the comparison determines two elements
1671
+ /// to be equally maximum.
1672
+ ///
1673
+ /// # Examples
1674
+ ///
1675
+ /// ```
1676
+ /// let a = [-3_i32, 0, 1, 5, -10];
1677
+ /// assert_eq!(*a.iter().max_by(|x, y| x.cmp(y)).unwrap(), 5);
1678
+ /// ```
1679
+ #[ inline]
1680
+ #[ unstable( feature = "iter_max_by" , issue="1722" ) ]
1681
+ fn max_by < F > ( self , mut compare : F ) -> Option < Self :: Item >
1682
+ where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
1683
+ {
1684
+ select_fold1 ( self ,
1685
+ |_| ( ) ,
1686
+ // switch to y even if it is only equal, to preserve
1687
+ // stability.
1688
+ |_, x, _, y| Ordering :: Greater != compare ( x, y) )
1689
+ . map ( |( _, x) | x)
1690
+ }
1691
+
1667
1692
/// Returns the element that gives the minimum value from the
1668
1693
/// specified function.
1669
1694
///
@@ -1688,6 +1713,32 @@ pub trait Iterator {
1688
1713
. map ( |( _, x) | x)
1689
1714
}
1690
1715
1716
+ /// Returns the element that gives the minimum value with respect to the
1717
+ /// specified comparison function.
1718
+ ///
1719
+ /// Returns the latest element if the comparison determines two elements
1720
+ /// to be equally minimum.
1721
+ ///
1722
+ /// # Examples
1723
+ ///
1724
+ /// ```
1725
+ /// let a = [-3_i32, 0, 1, 5, -10];
1726
+ /// assert_eq!(*a.iter().min_by(|x, y| x.cmp(y)).unwrap(), -10);
1727
+ /// ```
1728
+ #[ inline]
1729
+ #[ unstable( feature = "iter_min_by" , issue="1722" ) ]
1730
+ fn min_by < F > ( self , mut compare : F ) -> Option < Self :: Item >
1731
+ where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
1732
+ {
1733
+ select_fold1 ( self ,
1734
+ |_| ( ) ,
1735
+ // switch to y even if it is strictly smaller, to
1736
+ // preserve stability.
1737
+ |_, x, _, y| Ordering :: Greater == compare ( x, y) )
1738
+ . map ( |( _, x) | x)
1739
+ }
1740
+
1741
+
1691
1742
/// Reverses an iterator's direction.
1692
1743
///
1693
1744
/// Usually, iterators iterate from left to right. After using `rev()`,
0 commit comments