Skip to content

Commit 5ff4ef8

Browse files
authored
Merge pull request #385 from yjhmelody/stream-min_by_key
add stream::min_by_key method
2 parents f311e3d + 7cfec4e commit 5ff4ef8

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/stream/stream/min_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 MinByKeyFuture<S, T, K> {
14+
#[pin]
15+
stream: S,
16+
min: Option<T>,
17+
key_by: K,
18+
}
19+
}
20+
21+
impl<S, T, K> MinByKeyFuture<S, T, K> {
22+
pub(super) fn new(stream: S, key_by: K) -> Self {
23+
MinByKeyFuture {
24+
stream,
25+
min: None,
26+
key_by,
27+
}
28+
}
29+
}
30+
31+
impl<S, K> Future for MinByKeyFuture<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.min.take() {
48+
None => *this.min = Some(new),
49+
50+
Some(old) => match new.cmp(&old) {
51+
Ordering::Less => *this.min = Some(new),
52+
_ => *this.min = Some(old),
53+
},
54+
}
55+
Poll::Pending
56+
}
57+
None => Poll::Ready(this.min.take()),
58+
}
59+
}
60+
}

src/stream/stream/mod.rs

+39
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod le;
4141
mod lt;
4242
mod map;
4343
mod min_by;
44+
mod min_by_key;
4445
mod next;
4546
mod nth;
4647
mod partial_cmp;
@@ -69,6 +70,7 @@ use last::LastFuture;
6970
use le::LeFuture;
7071
use lt::LtFuture;
7172
use min_by::MinByFuture;
73+
use min_by_key::MinByKeyFuture;
7274
use next::NextFuture;
7375
use nth::NthFuture;
7476
use partial_cmp::PartialCmpFuture;
@@ -680,6 +682,43 @@ extension_trait! {
680682
FilterMap::new(self, f)
681683
}
682684

685+
#[doc = r#"
686+
Returns the element that gives the minimum value with respect to the
687+
specified key function. If several elements are equally minimum,
688+
the first element is returned. If the stream is empty, `None` is returned.
689+
690+
# Examples
691+
692+
```
693+
# fn main() { async_std::task::block_on(async {
694+
#
695+
use std::collections::VecDeque;
696+
697+
use async_std::prelude::*;
698+
699+
let s: VecDeque<i32> = vec![1, 2, -3].into_iter().collect();
700+
701+
let min = s.clone().min_by_key(|x| x.abs()).await;
702+
assert_eq!(min, Some(1));
703+
704+
let min = VecDeque::<isize>::new().min_by_key(|x| x.abs()).await;
705+
assert_eq!(min, None);
706+
#
707+
# }) }
708+
```
709+
"#]
710+
fn min_by_key<K>(
711+
self,
712+
key_by: K,
713+
) -> impl Future<Output = Option<Self::Item>> [MinByKeyFuture<Self, Self::Item, K>]
714+
where
715+
Self: Sized,
716+
Self::Item: Ord,
717+
K: FnMut(&Self::Item) -> Self::Item,
718+
{
719+
MinByKeyFuture::new(self, key_by)
720+
}
721+
683722
#[doc = r#"
684723
Returns the element that gives the minimum value with respect to the
685724
specified comparison function. If several elements are equally minimum,

0 commit comments

Comments
 (0)