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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::sync::Arc;
use Value;
fn top(stack: &mut [Box<dyn Process>]) -> *mut Box<dyn Process> {
stack.last_mut().unwrap() as *mut Box<dyn Process>
}
pub fn ingest (stack: &mut [Box<dyn Process>], v: Value) -> Option<Value> {
unsafe { (*top(stack)).ingest(stack, v) }
}
pub fn inges (stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
unsafe { (*top(stack)).inges(stack, v) }
}
pub fn ingest_kv(stack: &mut [Box<dyn Process>], k: Value, v: Value) -> Option<Value> {
unsafe { (*top(stack)).ingest_kv(stack, k, v) }
}
pub fn inges_kv (stack: &mut [Box<dyn Process>], k: &Value, v: &Value) -> Option<Value> {
unsafe { (*top(stack)).inges_kv(stack, k, v) }
}
pub fn last_call(stack: &mut [Box<dyn Process>]) -> Value {
unsafe { (*top(stack)).last_call(stack) }
}
pub trait Process {
fn ingest (&mut self, stack: &mut [Box<dyn Process>], v: Value) -> Option<Value> {
inges(stack, &v)
}
fn inges (&mut self, stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
ingest(stack, v.split_out())
}
fn ingest_kv(&mut self, stack: &mut [Box<dyn Process>], k: Value, v: Value) -> Option<Value> {
inges_kv(stack, &k, &v)
}
fn inges_kv (&mut self, stack: &mut [Box<dyn Process>], k: &Value, v: &Value) -> Option<Value> {
ingest_kv(stack, k.split_out(), v.split_out())
}
fn last_call(&mut self, stack: &mut [Box<dyn Process>]) -> Value {
let (_, rest) = stack.split_last_mut().unwrap();
last_call(rest)
}
}
pub struct Pass { }
impl Process for Pass {
fn ingest (&mut self, stack: &mut [Box<dyn Process>], v: Value) -> Option<Value> {
let (_, rest) = stack.split_last_mut().unwrap();
ingest(rest, v)
}
fn inges (&mut self, stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
let (_, rest) = stack.split_last_mut().unwrap();
inges(rest, v)
}
fn ingest_kv(&mut self, stack: &mut [Box<dyn Process>], k: Value, v: Value) -> Option<Value> {
let (_, rest) = stack.split_last_mut().unwrap();
ingest_kv(rest, k, v)
}
fn inges_kv (&mut self, stack: &mut [Box<dyn Process>], k: &Value, v: &Value) -> Option<Value> {
let (_, rest) = stack.split_last_mut().unwrap();
inges_kv(rest, k, v)
}
}
pub struct Xf<F> {
pub new_process: F,
}
impl<F: 'static + Fn() -> Box<dyn Process>> Transduce for Xf<F> {
fn process(&self) -> Box<dyn Process> {
(self.new_process)()
}
}
impl<F: 'static + Fn() -> Box<dyn Process>> Xf<F> {
pub fn new(f: F) -> Transducer {
Transducer { t: Arc::new(Xf { new_process: f }) }
}
}
pub fn filter(pred: fn(&Value) -> bool) -> Transducer {
struct Filter<G> { pred: G }
impl<G: Fn(&Value) -> bool> Process for Filter<G> {
fn ingest (&mut self, stack: &mut [Box<dyn Process>], v: Value) -> Option<Value> {
if (self.pred)(&v) {
let (_, rest) = stack.split_last_mut().unwrap();
ingest(rest, v)
} else {
None
}
}
fn inges (&mut self, stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
if (self.pred)(v) {
let (_, rest) = stack.split_last_mut().unwrap();
inges(rest, v)
} else {
None
}
}
}
Xf::new(move || Box::new(Filter { pred }))
}
pub trait Transduce {
fn process(&self) -> Box<dyn Process>;
fn transduce(&self, mut process_stack: Vec<Box<dyn Process>>) -> Vec<Box<dyn Process>> {
process_stack.push(self.process());
process_stack
}
fn info(&self) -> Option<String> {
None
}
}
#[derive(Clone)]
pub struct Transducer {
pub t: Arc<dyn Transduce>,
}
impl Transducer {
pub fn process(&self) -> Box<dyn Process> { self.t.process() }
pub fn transduce(&self, process_stack: Vec<Box<dyn Process>>) -> Vec<Box<dyn Process>> {
self.t.transduce(process_stack)
}
}
pub struct Transducers {
pub stack: Arc<Vec<Transducer>>,
}
impl Transducers {
pub fn new() -> Transducers {
Transducers { stack: Vec::new().into() }
}
pub fn add_transducer(&mut self, t: Transducer) {
Arc::make_mut(&mut self.stack).push(t)
}
pub fn apply(&self, mut process_stack: Vec<Box<dyn Process>>) -> Vec<Box<dyn Process>> {
for t in self.stack.iter().rev() {
process_stack = t.transduce(process_stack);
}
process_stack
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hello() {
}
}