use super::stream::{StageStep, Stream};
use crate::streaming::transducer::Transducer;
pub type StateStep<S, A, B> = Box<dyn FnMut(S, A) -> (S, Option<B>) + Send>;
pub fn state_scan<S, A, B, E, R>(
stream: Stream<A, E, R>,
init: S,
step: impl FnMut(S, A) -> (S, Option<B>) + Send + 'static,
) -> Stream<B, E, R>
where
S: Send + 'static,
A: Send + 'static,
B: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
stream.state_scan(init, step)
}
impl<A, E, R> Stream<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn transduce_items(self, xf: Transducer<A, Vec<A>>) -> Self {
self.stage(move |input| {
let mut out = Vec::new();
for item in input.unwrap_or_default() {
let push: crate::streaming::transducer::Reducer<A, Vec<A>> = Box::new(|mut acc, x| {
acc.push(x);
acc
});
out.extend(xf.transduce(std::iter::once(item), push, Vec::new()));
}
StageStep::Emit(out)
})
}
pub fn via_transducer(self, xf: Transducer<A, Vec<A>>) -> Self {
self.transduce_items(xf)
}
}