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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::Data;
use crate::dataflow::{Stream, Scope};
use crate::dataflow::channels::pact::Pipeline;
use crate::dataflow::operators::generic::operator::Operator;
pub trait Map<S: Scope, D: Data> {
fn map<D2: Data>(&self, logic: impl Fn(D)->D2+'static) -> Stream<S, D2>;
fn map_in_place(&self, logic: impl Fn(&mut D)+'static) -> Stream<S, D>;
fn flat_map<I: IntoIterator>(&self, logic: impl Fn(D)->I+'static) -> Stream<S, I::Item> where I::Item: Data;
}
impl<S: Scope, D: Data> Map<S, D> for Stream<S, D> {
fn map<D2: Data>(&self, logic: impl Fn(D)->D2+'static) -> Stream<S, D2> {
let mut vector = Vec::new();
self.unary(Pipeline, "Map", move |_,_| move |input, output| {
input.for_each(|time, data| {
data.swap(&mut vector);
output.session(&time).give_iterator(vector.drain(..).map(|x| logic(x)));
});
})
}
fn map_in_place(&self, logic: impl Fn(&mut D)+'static) -> Stream<S, D> {
let mut vector = Vec::new();
self.unary(Pipeline, "MapInPlace", move |_,_| move |input, output| {
input.for_each(|time, data| {
data.swap(&mut vector);
for datum in vector.iter_mut() { logic(datum); }
output.session(&time).give_vec(&mut vector);
})
})
}
fn flat_map<I: IntoIterator>(&self, logic: impl Fn(D)->I+'static) -> Stream<S, I::Item> where I::Item: Data {
let mut vector = Vec::new();
self.unary(Pipeline, "FlatMap", move |_,_| move |input, output| {
input.for_each(|time, data| {
data.swap(&mut vector);
output.session(&time).give_iterator(vector.drain(..).flat_map(|x| logic(x).into_iter()));
});
})
}
}