use std::sync::Arc;
use comp_cat_rs::effect::io::Io;
use crate::stage::Stage;
pub trait Hydrator<I, J, E> {
fn hydrate(&self, item: I) -> Io<E, J>;
}
pub fn hydrator_stage<H, I, J, E>(hydrator: H) -> Stage<E, Vec<I>, Vec<J>>
where
H: Hydrator<I, J, E> + Send + Sync + 'static,
I: Send + 'static,
J: Send + 'static,
E: Send + 'static,
{
let hydrator = Arc::new(hydrator);
Stage::new(move |items: Vec<I>| {
items.into_iter().fold(Io::pure(Vec::new()), |acc_io, item| {
let h = Arc::clone(&hydrator);
acc_io.flat_map(move |acc| {
h.hydrate(item).map(move |j| {
acc.into_iter().chain(std::iter::once(j)).collect()
})
})
})
})
}