1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use async_trait::async_trait;
use futures::stream::Stream;
use futures::stream::StreamExt;
use itertools::Combinations;
#[async_trait]
pub trait Combinable<T> {
async fn combinations(
self,
k: usize,
) -> futures::stream::Iter<Combinations<std::vec::IntoIter<T>>>;
}
#[async_trait]
impl<T, SInput> Combinable<T> for SInput
where
SInput: Stream<Item = T> + Send,
T: Clone + Send,
{
async fn combinations(
self,
k: usize,
) -> futures::stream::Iter<Combinations<std::vec::IntoIter<T>>> {
use itertools::Itertools;
let combinations_iterable = self.collect::<Vec<_>>().await.into_iter().combinations(k);
futures::stream::iter(combinations_iterable)
}
}
#[cfg(test)]
mod tests {
use super::Combinable;
use futures::stream::StreamExt;
#[tokio::test]
async fn combinations() {
assert_eq!(
futures::stream::iter(vec![1, 2, 3])
.combinations(2)
.await
.collect::<Vec<_>>()
.await,
vec![vec![1, 2], vec![1, 3], vec![2, 3],]
);
}
}