File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -1750,6 +1750,29 @@ pub trait Itertools : Iterator {
1750
1750
self . find_map ( |x| if predicate ( & x) { Some ( x) } else { prev = Some ( x) ; None } )
1751
1751
. or ( prev)
1752
1752
}
1753
+ /// Find the value of the first element satisfying a predicate or return the first element, if any.
1754
+ ///
1755
+ /// The iterator is not advanced past the first element found.
1756
+ ///
1757
+ /// ```
1758
+ /// use itertools::Itertools;
1759
+ ///
1760
+ /// let numbers = [1, 2, 3, 4];
1761
+ /// assert_eq!(numbers.iter().find_or_first(|&&x| x > 5), Some(&1));
1762
+ /// assert_eq!(numbers.iter().find_or_first(|&&x| x > 2), Some(&3));
1763
+ /// assert_eq!(std::iter::empty::<i32>().find_or_first(|&x| x > 5), None);
1764
+ /// ```
1765
+ fn find_or_first < P > ( mut self , mut predicate : P ) -> Option < Self :: Item >
1766
+ where Self : Sized ,
1767
+ P : FnMut ( & Self :: Item ) -> bool ,
1768
+ {
1769
+ let first = self . next ( ) ?;
1770
+ Some ( if predicate ( & first) {
1771
+ first
1772
+ } else {
1773
+ self . find ( |x| predicate ( & x) ) . unwrap_or ( first)
1774
+ } )
1775
+ }
1753
1776
/// Returns `true` if the given item is present in this iterator.
1754
1777
///
1755
1778
/// This method is short-circuiting. If the given item is present in this
You can’t perform that action at this time.
0 commit comments