-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
48 lines (41 loc) · 1.17 KB
/
lib.rs
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
#![feature(associated_type_defaults)]
pub mod minmax;
pub use minmax::*;
pub struct Pipeline<I, O> {
steps: Vec<Box<dyn Transformer<I, O, Input = I, Output = O>>>,
}
impl<I, O> Pipeline<I, O> {
pub fn new(steps: Vec<Box<dyn Transformer<I, O, Input = I, Output = O>>>) -> Self {
Self { steps }
}
pub fn steps(&self) -> &Vec<Box<dyn Transformer<I, O, Input = I, Output = O>>> {
&self.steps
}
}
impl<I, O> Transformer<I, O> for Pipeline<I, O>
where
Vec<I>: From<Vec<O>> + Clone,
Vec<O>: From<Vec<I>>,
{
type Input = I;
type Output = O;
fn fit(&mut self, x: Vec<I>) {
self.steps.iter_mut().fold(x, |acc, transformer| {
transformer.fit(acc.clone());
transformer.transform(acc.into()).into()
});
}
fn transform(&self, x: Vec<I>) -> Vec<O> {
let out = self
.steps
.iter()
.fold(x, |out, transformer| transformer.transform(out).into());
out.into()
}
}
pub trait Transformer<I, O> {
type Input = I;
type Output = O;
fn fit(&mut self, x: Vec<Self::Input>);
fn transform(&self, x: Vec<Self::Input>) -> Vec<Self::Output>;
}