pub fn scan<I: 'static, O: 'static, F: 'static, S>(
reducer: F,
seed: O
) -> Box<dyn Fn(S) -> Source<O>> where
O: Send + Sync + Clone,
F: Fn(O, I) -> O + Send + Sync + Clone,
S: Into<Arc<Source<I>>>,
Expand description
Callbag operator that combines consecutive values from the same source.
It’s essentially like Iterator::scan
, delivering a new accumulated value for each value
from the callbag source.
Works on either pullable or listenable sources.
Examples
use crossbeam_queue::SegQueue;
use std::sync::Arc;
use callbag::{for_each, from_iter, scan};
let actual = Arc::new(SegQueue::new());
let iter_source = from_iter([1, 2, 3, 4, 5]);
let scanned = scan(|prev, x| prev + x, 0)(iter_source);
for_each({
let actual = Arc::clone(&actual);
move |x| {
println!("{}", x);
actual.push(x);
}
})(scanned);
assert_eq!(
&{
let mut v = vec![];
for _i in 0..actual.len() {
v.push(actual.pop().unwrap());
}
v
}[..],
[1, 3, 6, 10, 15]
);