Skip to content

Commit 3a2e6d5

Browse files
yjhmelodyStjepan Glavina
authored and
Stjepan Glavina
committed
add max_by_key (#408)
* add max_by_key * fix conflict * fmt code
1 parent 5fb9d3e commit 3a2e6d5

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/stream/stream/max_by_key.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::cmp::Ordering;
2+
use std::pin::Pin;
3+
4+
use pin_project_lite::pin_project;
5+
6+
use crate::future::Future;
7+
use crate::stream::Stream;
8+
use crate::task::{Context, Poll};
9+
10+
pin_project! {
11+
#[doc(hidden)]
12+
#[allow(missing_debug_implementations)]
13+
pub struct MaxByKeyFuture<S, T, K> {
14+
#[pin]
15+
stream: S,
16+
max: Option<T>,
17+
key_by: K,
18+
}
19+
}
20+
21+
impl<S, T, K> MaxByKeyFuture<S, T, K> {
22+
pub(super) fn new(stream: S, key_by: K) -> Self {
23+
Self {
24+
stream,
25+
max: None,
26+
key_by,
27+
}
28+
}
29+
}
30+
31+
impl<S, K> Future for MaxByKeyFuture<S, S::Item, K>
32+
where
33+
S: Stream,
34+
K: FnMut(&S::Item) -> S::Item,
35+
S::Item: Ord,
36+
{
37+
type Output = Option<S::Item>;
38+
39+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
40+
let this = self.project();
41+
let next = futures_core::ready!(this.stream.poll_next(cx));
42+
43+
match next {
44+
Some(new) => {
45+
let new = (this.key_by)(&new);
46+
cx.waker().wake_by_ref();
47+
match this.max.take() {
48+
None => *this.max = Some(new),
49+
50+
Some(old) => match new.cmp(&old) {
51+
Ordering::Greater => *this.max = Some(new),
52+
_ => *this.max = Some(old),
53+
},
54+
}
55+
Poll::Pending
56+
}
57+
None => Poll::Ready(this.max.take()),
58+
}
59+
}
60+
}

src/stream/stream/mod.rs

+39
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod le;
4343
mod lt;
4444
mod map;
4545
mod max_by;
46+
mod max_by_key;
4647
mod min;
4748
mod min_by;
4849
mod min_by_key;
@@ -77,6 +78,7 @@ use last::LastFuture;
7778
use le::LeFuture;
7879
use lt::LtFuture;
7980
use max_by::MaxByFuture;
81+
use max_by_key::MaxByKeyFuture;
8082
use min::MinFuture;
8183
use min_by::MinByFuture;
8284
use min_by_key::MinByKeyFuture;
@@ -767,6 +769,43 @@ extension_trait! {
767769
MinByKeyFuture::new(self, key_by)
768770
}
769771

772+
#[doc = r#"
773+
Returns the element that gives the maximum value with respect to the
774+
specified key function. If several elements are equally maximum,
775+
the first element is returned. If the stream is empty, `None` is returned.
776+
777+
# Examples
778+
779+
```
780+
# fn main() { async_std::task::block_on(async {
781+
#
782+
use std::collections::VecDeque;
783+
784+
use async_std::prelude::*;
785+
786+
let s: VecDeque<i32> = vec![-1, -2, -3].into_iter().collect();
787+
788+
let max = s.clone().max_by_key(|x| x.abs()).await;
789+
assert_eq!(max, Some(3));
790+
791+
let max = VecDeque::<isize>::new().max_by_key(|x| x.abs()).await;
792+
assert_eq!(max, None);
793+
#
794+
# }) }
795+
```
796+
"#]
797+
fn max_by_key<K>(
798+
self,
799+
key_by: K,
800+
) -> impl Future<Output = Option<Self::Item>> [MaxByKeyFuture<Self, Self::Item, K>]
801+
where
802+
Self: Sized,
803+
Self::Item: Ord,
804+
K: FnMut(&Self::Item) -> Self::Item,
805+
{
806+
MaxByKeyFuture::new(self, key_by)
807+
}
808+
770809
#[doc = r#"
771810
Returns the element that gives the minimum value with respect to the
772811
specified comparison function. If several elements are equally minimum,

0 commit comments

Comments
 (0)