@@ -40,6 +40,7 @@ mod last;
40
40
mod le;
41
41
mod lt;
42
42
mod map;
43
+ mod max_by;
43
44
mod min_by;
44
45
mod min_by_key;
45
46
mod next;
@@ -69,6 +70,7 @@ use gt::GtFuture;
69
70
use last:: LastFuture ;
70
71
use le:: LeFuture ;
71
72
use lt:: LtFuture ;
73
+ use max_by:: MaxByFuture ;
72
74
use min_by:: MinByFuture ;
73
75
use min_by_key:: MinByKeyFuture ;
74
76
use next:: NextFuture ;
@@ -758,6 +760,45 @@ extension_trait! {
758
760
MinByFuture :: new( self , compare)
759
761
}
760
762
763
+ #[ doc = r#"
764
+ Returns the element that gives the maximum value with respect to the
765
+ specified comparison function. If several elements are equally maximum,
766
+ the first element is returned. If the stream is empty, `None` is returned.
767
+
768
+ # Examples
769
+
770
+ ```
771
+ # fn main() { async_std::task::block_on(async {
772
+ #
773
+ use std::collections::VecDeque;
774
+
775
+ use async_std::prelude::*;
776
+
777
+ let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
778
+
779
+ let max = s.clone().max_by(|x, y| x.cmp(y)).await;
780
+ assert_eq!(max, Some(3));
781
+
782
+ let max = s.max_by(|x, y| y.cmp(x)).await;
783
+ assert_eq!(max, Some(1));
784
+
785
+ let max = VecDeque::<usize>::new().max_by(|x, y| x.cmp(y)).await;
786
+ assert_eq!(max, None);
787
+ #
788
+ # }) }
789
+ ```
790
+ "# ]
791
+ fn max_by<F >(
792
+ self ,
793
+ compare: F ,
794
+ ) -> impl Future <Output = Option <Self :: Item >> [ MaxByFuture <Self , F , Self :: Item >]
795
+ where
796
+ Self : Sized ,
797
+ F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
798
+ {
799
+ MaxByFuture :: new( self , compare)
800
+ }
801
+
761
802
#[ doc = r#"
762
803
Returns the nth element of the stream.
763
804
0 commit comments